Skip to content

Lexical fields drawer: Save is silently refused when the debounced onChange aborts the submit's form-state request #19385

Open

Description

@jdbarrancsc

Describe the Bug

The Lexical fields drawer (DrawerContent, used by the link drawer and the other lexical_internal_feature drawers) passes one callback to its Form as both onChange and beforeSubmit. That callback aborts and replaces one shared AbortController ref (onChangeAbortControllerRef) before each form-state request.

If the user presses Save while the submit's form-state request is still in flight, the Form's debounced onChange (250 ms after the last keystroke) calls the same callback, and that call aborts the submit's request. getFormState then resolves to { state: null }, so the callback falls back to the client form state it was given. That state still carries the valid flag from the last completed server validation, not from the value just typed.

When that last completed validation marked a field invalid, Form.submit returns early through its !isValid branch. A common case is the toolbar's https:// placeholder under a validate on the link url field. The drawer stays open, no field error is shown, and the link is not applied. A second click on Save works, so users experience it as "Save does nothing sometimes".

Root cause, in packages/richtext-lexical/src/utilities/fieldsDrawer/DrawerContent.tsx (v3.93.0):

  • line 38: const onChangeAbortControllerRef = useRef(new AbortController())
  • lines 99-104: onChange calls abortAndIgnore(onChangeAbortControllerRef.current) and stores its new controller in the same ref
  • line 156: beforeSubmit={[onChange]}
  • line 160: onChange={[onChange]}

The same code is inlined in the published client bundle (dist/exports/client/chunk-*.js). The sequence was confirmed by instrumenting AbortController.prototype.abort:

  1. The Save click runs beforeSubmit, which stores a new controller for the submit's request.
  2. About 210 ms later, the debounced executeOnChange (packages/ui/src/forms/Form/index.tsx around line 865) calls the same callback.
  3. That call aborts the submit's controller.

Suggested fix: give beforeSubmit its own AbortController, so the debounced onChange can never abort a submit's request. The onChange path keeps its current behaviour, where a newer change aborts an older one:

const onChangeAbortControllerRef = useRef(new AbortController())
const beforeSubmitAbortControllerRef = useRef(new AbortController())

const onChange = useCallback(
  async (
    { formState: prevFormState }: { formState: FormState },
    abortControllerRef = onChangeAbortControllerRef,
  ) => {
    abortAndIgnore(abortControllerRef.current)
    const controller = new AbortController()
    abortControllerRef.current = controller
    // ...unchanged getFormState call using controller.signal...
  },
  [/* unchanged */],
)

const beforeSubmit = useCallback(
  (args: { formState: FormState }) => onChange(args, beforeSubmitAbortControllerRef),
  [onChange],
)

useEffect(() => {
  return () => {
    abortAndIgnore(onChangeAbortControllerRef.current)
    abortAndIgnore(beforeSubmitAbortControllerRef.current)
  }
}, [])

// <Form beforeSubmit={[beforeSubmit]} onChange={[onChange]} ... />

Form calls every beforeSubmit and onChange function with a single object argument, so the defaulted second parameter is safe. We carry exactly this change as a pnpm patch against 3.90.2. With it, our regression test passes 10 of 10 runs; without it, the test fails every run.

Link to the code that reproduces this issue

return (
<Form
beforeSubmit={[onChange]}
disableValidationOnSubmit
fields={Array.isArray(fields) ? fields : []}
initialState={initialState}
onChange={[onChange]}
onSubmit={handleDrawerSubmit}
uuid={uuid()}

Reproduction Steps

The bug is in Payload's own drawer, so a blank app (pnpx create-payload-app@latest -t blank) reproduces it with one field change:

  1. Give the link feature's url field a validation that rejects the placeholder:

    lexicalEditor({
      features: ({ defaultFeatures }) => [
        ...defaultFeatures.filter((feature) => feature.key !== 'link'),
        LinkFeature({
          fields: ({ defaultFields }) =>
            defaultFields.map((field) =>
              'name' in field && field.name === 'url'
                ? { ...field, validate: (value: unknown) => (value && value !== 'https://' ? true : 'Enter a URL') }
                : field,
            ),
        }),
      ],
    })
  2. Open a document with a rich text field, type some text, select it, and click the toolbar's Link button. The drawer opens with URL = https://.

  3. Slow down form-state requests, with about 400 ms of latency on the drawer's form-state server-function POST. Chrome DevTools "Slow 4G" works by hand. With Playwright:

    await context.route(`**/admin/collections/pages/${id}`, async (route) => {
      const body = route.request().postData() ?? ''
      if (route.request().method() === 'POST' && body.includes('"form-state"') && body.includes('lexical_internal_feature')) {
        await new Promise((resolve) => setTimeout(resolve, 400))
      }
      await route.continue().catch(() => undefined)
    })
  4. Click into the URL field, select all, type https://example.com/records (30 ms between keystrokes), and click Save immediately after the last keystroke.

Expected: the drawer closes, and the selected text becomes a link to https://example.com/records.

Actual: the drawer stays open with the typed URL, no error is shown, and no link is created. In our runs this happened in 19 of 20 attempts at 400 ms latency.

Which area(s) are affected?

plugin: richtext-lexical, area: ui

Environment Info

Reproduced on:  @payloadcms/richtext-lexical 3.90.2 and @payloadcms/ui 3.90.2
Still present:  3.93.0 (checked in the published dist and the v3.93.0 source)
Node.js:        24.22.1
Next.js:        16.4.2
React:          19.2.8
Database:       @payloadcms/db-postgres (Postgres 18)
Browser:        Chromium (Playwright 1.62.1)

Activity

  1. added
    area: ui
    plugin: richtext-lexical
    on Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugPayload OSSarea: uiplugin: richtext-lexical

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions