Description
Describe the Bug
With drafts and autosave on, an edit made while an autosave is in flight is sometimes never saved. The field shows the edit and the saving indicator finishes, but a reload shows the document as it was one edit earlier. It happens when a second autosave was queued behind the first, and the edit is made during that second save. We run collections with autosave.interval at 100 ms, and editors lose their last keystroke intermittently.
The cause is in useQueue (packages/ui/src/hooks/useQueue.ts). processQueue calls options.beforeProcess() once, before its while loop, but calls options.afterProcess() after every task in the loop. Autosave passes setBackgroundProcessing(true) and setBackgroundProcessing(false) as those two callbacks (packages/ui/src/elements/Autosave/index.tsx). So a task that was queued while the first one ran starts after afterProcess has set background processing back to false, and nothing sets it to true again.
The form relies on that flag. setModified in packages/ui/src/forms/Form/index.tsx records modifiedWhileProcessingRef only while backgroundProcessingRef is true, and submit calls setModified(false) after the response unless that ref is set. The sequence is:
- Autosave 1 is in flight. The editor types, and autosave 2 is queued behind it.
- Autosave 1 returns.
afterProcesssets background processing tofalse, and the loop runs autosave 2 without setting it again. - The editor types during autosave 2.
setModified(true)doesn't record it as modified while processing, because the flag isfalse. - Autosave 2 returns and calls
setModified(false). - The debounce for the last edit fires.
handleAutosavequeues a task that saves onlyif (modified), andmodifiedis nowfalse, so nothing is saved.
Moving the beforeProcess call inside the loop, so it runs before each task as afterProcess runs after each one, fixes it. A pull request with that change and a test follows.
Link to the code that reproduces this issue
That test reproduces the queue's part of it: it fails on main without the fix. The whole sequence needs two autosave requests to overlap, which the steps below arrange by holding each request.
Reproduction Steps
In a Playwright test against the admin, with a collection whose autosave interval is 100 ms:
- Route the document's autosave
PATCHrequests so each one is held until the test releases it, and installpage.clockso the debounce fires only when the test advances it. - Type a character into a text field and advance the clock past the interval. Autosave 1 starts and is held.
- Type a second character and advance the clock. Autosave 2 is queued behind autosave 1. Release autosave 1, and autosave 2 starts and is held.
- Type a third character, release autosave 2, and wait for its response. Then advance the clock past the interval.
- Read the document's draft through the REST API.
Expected: the draft has all three characters. Actual: it has the first two, and no request was sent for the third, although the field shows it. With the fix, a third autosave is sent and the draft has all three. We ran this against 3.90.2 with and without the change.
Which area(s) are affected?
area: ui
Environment Info
Payload: 3.90.2
Next.js: 16.3.6
React: 19.3.0
Node.js: 24
useQueue is the same on main (v4 canary) and 3.x.
🤖 Generated with Claude Code
Activity
fix(ui)!: consolidate all form request types into a single queue (#19203
github-actions commentedon Nov 17, 2026
🚀 This is included in version v4.0.0-beta.10
fix(ui): prevent stale responses from overwriting form state (#17455)