Skip to content

Autosave drops an edit made while a queued autosave is in flight #18348

Closed

Description

@adrian-baker

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:

  1. Autosave 1 is in flight. The editor types, and autosave 2 is queued behind it.
  2. Autosave 1 returns. afterProcess sets background processing to false, and the loop runs autosave 2 without setting it again.
  3. The editor types during autosave 2. setModified(true) doesn't record it as modified while processing, because the flag is false.
  4. Autosave 2 returns and calls setModified(false).
  5. The debounce for the last edit fires. handleAutosave queues a task that saves only if (modified), and modified is now false, 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

https://github.com/adrian-baker/payload/blob/e5591de42fdd3e413189b70248b71ccc35c57060/packages/ui/src/hooks/useQueue.spec.tsx

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:

  1. Route the document's autosave PATCH requests so each one is held until the test releases it, and install page.clock so the debounce fires only when the test advances it.
  2. Type a character into a text field and advance the clock past the interval. Autosave 1 starts and is held.
  3. 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.
  4. Type a third character, release autosave 2, and wait for its response. Then advance the clock past the interval.
  5. 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

  1. added
    area: ui
    on Sep 28, 2026
  2. added a commit that references this issue on Nov 16, 2026
    20ac538
  3. github-actions commented on Nov 17, 2026

    @github-actions
    Contributor

    🚀 This is included in version v4.0.0-beta.10

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: ui

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions