Repository navigation
Description
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:
onChangecallsabortAndIgnore(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:
- The Save click runs
beforeSubmit, which stores a new controller for the submit's request. - About 210 ms later, the debounced
executeOnChange(packages/ui/src/forms/Form/index.tsxaround line 865) calls the same callback. - 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
payload/packages/richtext-lexical/src/utilities/fieldsDrawer/DrawerContent.tsx
Lines 154 to 162 in 77425c5
| 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:
-
Give the link feature's
urlfield 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, ), }), ], })
-
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://. -
Slow down form-state requests, with about 400 ms of latency on the drawer's
form-stateserver-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) })
-
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