Skip to content

[lexical-website] Documentation Update: setEditorState warns and recovers in production, it does not always throw - #9228

Merged
etrepum merged 1 commit into
meta:mainfrom
potatowagon:docs-editor-state-empty-recovery
Sep 24, 2026
Merged

etrepum merged 1 commit into
meta:mainfrom
potatowagon:docs-editor-state-empty-recovery

Conversation

@potatowagon

Copy link
Copy Markdown
Contributor

Description

The warning box in concepts/editor-state.md says setEditorState "throws"
when given an EditorState that satisfies isEmpty(), and that persisting
and reloading such a state "will fail at the setEditorState call".

That is only true in a development build. The check is a devInvariant: it
throws in development, but in production it logs the same message as a
warning and then recovers by appending an empty ParagraphNode, leaving the
editor usable. A reader following the current text expects a hard failure in
production and may add a workaround for a crash that does not happen there.

The two suggested workarounds are still worth doing — they avoid relying on
the recovery — so they stay, reworded.

Test plan

Documentation-only change (markdown is excluded from prettier via
.prettierignore), so the check is the behaviour of the code path.

Before

$ sed -n '200,201p' packages/lexical-website/docs/concepts/editor-state.md
`setEditorState` throws when called with an `EditorState` that satisfies
`editorState.isEmpty()` — i.e. the root is the only node and there is no selection. The

$ sed -n '1793,1803p' packages/lexical/src/LexicalEditor.ts
        if (isEmptyEditorState) {
          // A root with no children is not the canonical empty document: it
          // reconciles to a contenteditable with no block element to place a
          // caret in. It still arrives from outside the editor, because
          // content persisted while the editor was empty round-trips to
          // `{"root":{"children":[]}}`, so recover rather than leave the
          // editor unusable. Reusing the wording of the invariant this
          // replaces keeps the existing error code.
          devInvariant(
            false,
            "setEditorState: the editor state is empty. ...",

$ sed -n '1803p;1803,1804p' packages/lexical/src/LexicalEditor.ts | tail -1
          $getRoot().append($createParagraphNode());

$ sed -n '30,36p' packages/lexical-internal/src/devInvariant.ts
  if (__DEV__) {
    throw new Error(formatted);
  } else {
    console.warn(formatted);
  }

After

$ sed -n '200,204p' packages/lexical-website/docs/concepts/editor-state.md
`setEditorState` rejects an `EditorState` that satisfies `editorState.isEmpty()` — i.e.
the root is the only node and there is no selection. In a development build it throws
`"setEditorState: the editor state is empty. Ensure the editor state's root node never
becomes empty."`; in production it warns with the same message and recovers by appending
an empty `ParagraphNode`, so the editor stays usable. The `EditorState` produced by

The recovery ($getRoot().append($createParagraphNode()) immediately after
the devInvariant) is packages/lexical/src/LexicalEditor.ts:1793-1803, and
the dev-throws / prod-warns split is
packages/lexical-internal/src/devInvariant.ts:29-35.

…vers in production, it does not always throw

## Description

The warning box in `concepts/editor-state.md` says `setEditorState` "throws"
when given an `EditorState` that satisfies `isEmpty()`, and that persisting
and reloading such a state "will fail at the `setEditorState` call".

That is only true in a development build. The check is a `devInvariant`: it
throws in development, but in production it logs the same message as a
warning and then recovers by appending an empty `ParagraphNode`, leaving the
editor usable. A reader following the current text expects a hard failure in
production and may add a workaround for a crash that does not happen there.

The two suggested workarounds are still worth doing — they avoid relying on
the recovery — so they stay, reworded.

## Test plan

Documentation-only change (markdown is excluded from prettier via
`.prettierignore`), so the check is the behaviour of the code path.

### Before

```
$ sed -n '200,201p' packages/lexical-website/docs/concepts/editor-state.md
`setEditorState` throws when called with an `EditorState` that satisfies
`editorState.isEmpty()` — i.e. the root is the only node and there is no selection. The

$ sed -n '1793,1803p' packages/lexical/src/LexicalEditor.ts
        if (isEmptyEditorState) {
          // A root with no children is not the canonical empty document: it
          // reconciles to a contenteditable with no block element to place a
          // caret in. It still arrives from outside the editor, because
          // content persisted while the editor was empty round-trips to
          // `{"root":{"children":[]}}`, so recover rather than leave the
          // editor unusable. Reusing the wording of the invariant this
          // replaces keeps the existing error code.
          devInvariant(
            false,
            "setEditorState: the editor state is empty. ...",

$ sed -n '1803p;1803,1804p' packages/lexical/src/LexicalEditor.ts | tail -1
          $getRoot().append($createParagraphNode());

$ sed -n '30,36p' packages/lexical-internal/src/devInvariant.ts
  if (__DEV__) {
    throw new Error(formatted);
  } else {
    console.warn(formatted);
  }
```

### After

```
$ sed -n '200,204p' packages/lexical-website/docs/concepts/editor-state.md
`setEditorState` rejects an `EditorState` that satisfies `editorState.isEmpty()` — i.e.
the root is the only node and there is no selection. In a development build it throws
`"setEditorState: the editor state is empty. Ensure the editor state's root node never
becomes empty."`; in production it warns with the same message and recovers by appending
an empty `ParagraphNode`, so the editor stays usable. The `EditorState` produced by
```

The recovery (`$getRoot().append($createParagraphNode())` immediately after
the `devInvariant`) is `packages/lexical/src/LexicalEditor.ts:1793-1803`, and
the dev-throws / prod-warns split is
`packages/lexical-internal/src/devInvariant.ts:29-35`.
@vercel

vercel Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
lexical Ready Ready Preview Sep 24, 2026 4:48pm UTC
lexical-playground Ready Ready Preview Sep 24, 2026 4:48pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 24, 2026
@etrepum
etrepum added this pull request to the merge queue Sep 24, 2026
Merged via the queue into meta:main with commit 2f82f37 Sep 24, 2026
42 checks passed
@etrepum etrepum mentioned this pull request Sep 28, 2026

This branch was successfully deployed

2 active deployments
Preview – lexical — 6341406e Deployed Sep 24, 2026 by vercel[bot]
Preview – lexical-playground — 6341406e Deployed Sep 24, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants