Skip to content

Lexical blocks: Save Draft submits stale fields while a child form-state response is pending (3.89.0) #18907

Open

Description

@Nijat-Hamid

Describe the Bug

In Payload 3.89.0, the enabled native Save Draft button can submit an earlier Lexical block value while a later edit in another block is still waiting for its nested Form's form-state response.

A deterministic component-level reproduction mounts the public RichTextField and BlocksFeatureClient, real Lexical BlockNodes/BlockComponents, their nested Payload Forms and TextFields, a parent document Form, and the native SaveDraftButton. No editor, field, node, provider, or submit implementation is patched or replaced.

Block A is edited and allowed to reach the parent document Form first. The parent is genuinely modified, so Save Draft is enabled in update mode. Block B's actual input is then edited, but its local form-state transport response is held. Clicking the native button sends a draft PATCH containing A's updated caption and B's previous caption, without waiting for B's response.

After releasing that response and letting the native block/editor/idle propagation complete, another native Save Draft sends both updated captions.

Expected: saving should either coordinate pending child-block changes before taking the document snapshot, or keep the save action unavailable until those changes have reached the document Form.

Actual: an enabled Save Draft completes with the earlier document snapshot while the later child-block change is pending.

Link to the code that reproduces this issue

https://gist.github.com/Nijat-Hamid/b38b76255255bf31902d5425913c6231

The reproduction contains only synthetic captions and minimal test configuration. Save its five files together in a new empty directory, use Node.js 24.18.0 or later, and run npm install followed by npm test. It also passed after a separate fresh npm install --ignore-scripts --no-audit --no-fund; it does not rely on a pre-existing application's dependency installation.

Reproduction Steps

  1. Run the linked characterization test. It seeds two instances of a minimal Lexical block with one text caption field each.

  2. Edit block A's real input to First A caption edit. Complete its real child Form debounce, the local form-state response, BlockComponent node update, and outer RichText idle callback. Assert the parent is modified and holds A's updated value.

  3. Edit block B's distinct real input to Second B caption edit. Let the real child Form invoke the local form-state transport, but hold that response unresolved. The test also checks that A's input did not change.

  4. Click the actual enabled #action-save-draft button. Capture its native PATCH FormData and inspect _payload. The caption projection is:

    [First A caption edit, Original B caption]
    
  5. Release B's form-state response, then let the actual BlockComponent setFields update and outer RichText idle callback finish. Click native Save Draft again. Its next _payload contains:

    [First A caption edit, Second B caption edit]
    

The test intentionally passes when it observes this ordering gap; that is a characterization, not the desired fixed behavior. The child response is deferred to make the ordering deterministic, not to implement an application workaround.

Scope of the reproduction

  • The public UI/editor path and real input/button interactions run in jsdom. CSS is inert; normal-shaped browser observers and Next navigation context values are supplied by the harness.
  • Only server-function/fetch transport is synthetic: form-state returns the supplied field state, preference lookups return local 404s, and draft PATCHes return synthetic HTTP 200 responses. Real sockets and HTTP are blocked.
  • No database, CMS server, environment file, account, or real content is involved. This does not establish a production data-loss incident.
  • The complete hosted Edit view's onChange/onSave, real server normalization/validation, database persistence/reload, Autosave and Publish are not tested. No claim is made that those other save paths were reproduced or fixed.

Source trace in the pinned packages

The child Form's debounced onChange awaits getFormState; BlockComponent then schedules editor.update(() => node.setFields(...)). The outer RichText Field defers its parent useField.setValue through useRunDeprioritized (idle timeout 500ms). Native Save Draft invokes the parent Form.submit, which snapshots its current fields before its beforeSubmit callbacks.

I could not find a documented public barrier that waits for both pending child Form responses and the outer RichText-to-Form bridge before that snapshot. An editor-state read alone would not await the child Form's request. If there is a supported coordination hook I have missed, guidance would be appreciated.

Possibly adjacent: #18425, but this report concerns nested Lexical block values in the outgoing document save, rather than late condition/validation metadata merging.

Which area(s) are affected?

area: ui; plugin: richtext-lexical

Environment Info

Payload / @payloadcms/ui / @payloadcms/next / @payloadcms/richtext-lexical: 3.89.0
@payloadcms/translations: 3.89.0
Lexical (installed dependency): 0.41.0
Next.js: 16.3.5
React / React DOM: 19.3.0
Node.js: 24.18.0
Vitest: 5.0.0
jsdom: 30.0.1
Vite: 8.1.5

These are the tested versions, not a claim about the latest v3/v4 release. No package patch or workaround has been applied.

Activity

  1. aayushbaluni commented on Oct 23, 2026

    @aayushbaluni

    On your question — "if there is a supported coordination hook I have missed" — beforeSubmit is the one that looks like it, and on current main (57f3909) it cannot serve as one. Two things in packages/ui/src/forms/Form/index.tsx stand in the way, and they are worth knowing before anyone designs the barrier:

    1. The outgoing payload is snapshotted before beforeSubmit runs.

    346:  const data = reduceFieldsToValues(contextRef.current.fields, true)
    ...
    356:  if (Array.isArray(beforeSubmit)) {
    ...
    424:  const formData = await contextRef.current.createFormData(overrides, { data, ... })
    

    data is taken at 346 and is the object handed to createFormData at 424. Nothing between those lines can change it except overrides. So a beforeSubmit hook that flushed pending child-block state would update the form state but not the request body.

    2. beforeSubmit's returned state is discarded on the success path.

    366:  revalidatedFormState = result
    369:  const isValid = Object.entries(revalidatedFormState).every(([, f]) => f.valid !== false)
    ...
    379:  return dispatchFields({ type: 'REPLACE_STATE', state: revalidatedFormState })   // invalid only
    

    revalidatedFormState is dispatched only when validation fails. When it passes it is read for valid and dropped, so the hook cannot contribute values even to the form state.

    3. Nothing registers a parent-level beforeSubmit for the editor. The three beforeSubmit call sites in richtext-lexical are all on nested forms — features/blocks/client/component/index.tsx:815, features/blocks/client/componentInline/index.tsx:509, and utilities/fieldsDrawer/DrawerContent.tsx:156. The parent document Form receives whatever the Edit view passes, which is not an editor flush.

    For completeness: the only await between the click and the snapshot is the conditional await wait(100) at 342-343 under waitForAutocomplete, so in the default path the snapshot is synchronous with the click. That matches the ordering your reproduction observes, and it means a fix cannot rely on incidental scheduling slack.

    That leaves the two strategies you named, and the choice between them is a product decision rather than a bug fix:

    • Await pending child state before the snapshot. Needs a registry of in-flight child form-state requests plus the useRunDeprioritized bridge in richtext-lexical/src/field/Field.tsx, which the parent submit can await before line 346. The deprioritized bridge is the awkward half: runDeprioritized already returns a promise, but the latest-invocation-wins cancellation means the promise for a superseded run never resolves, so a naive await there can hang.
    • Keep the action unavailable until child state has landed. Cheaper and easier to reason about, but it makes Save Draft flicker disabled during normal typing unless the pending window is debounced, which is a UX call.

    To be clear about what I checked: the three points above are read off main at 57f3909 and the line numbers are from that commit. I have not run the linked reproduction — the ordering it reports is consistent with the code path, but I am reporting the source trace, not a second reproduction.

    I am happy to implement either strategy, including the flush-promise fix the first one needs. Which would you accept? I would rather not guess at the semantics in a PR to the shared save path.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions