Repository navigation
[lexical-website] Documentation Update: setEditorState warns and recovers in production, it does not always throw - #9228
Merged
Conversation
…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`.
potatowagon
requested review from
acywatson,
etrepum,
fantactuka,
ivailop7 and
zurfyx
as code owners
September 24, 2026 16:47
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
etrepum
approved these changes
Sep 24, 2026
Merged
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The warning box in
concepts/editor-state.mdsayssetEditorState"throws"when given an
EditorStatethat satisfiesisEmpty(), and that persistingand reloading such a state "will fail at the
setEditorStatecall".That is only true in a development build. The check is a
devInvariant: itthrows in development, but in production it logs the same message as a
warning and then recovers by appending an empty
ParagraphNode, leaving theeditor 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
After
The recovery (
$getRoot().append($createParagraphNode())immediately afterthe
devInvariant) ispackages/lexical/src/LexicalEditor.ts:1793-1803, andthe dev-throws / prod-warns split is
packages/lexical-internal/src/devInvariant.ts:29-35.