Skip to content

Autosave / form-state response overwrites edits made while the request was in flight #19797

Closed

Description

@jonathanbharding

Describe the Bug

With drafts and autosave on, a form-state response can overwrite a value the editor changed while that request was in flight. Two cases:

  1. Text typed into a new array row is cleared. Add an array row, type into its text field, then (before the row's own form-state request returns, e.g. while an upload drawer saves) the response arrives and the text field comes back empty.
  2. A block row added during an autosave disappears or loses values. Edit a field, then immediately Add Block. When the autosave sent before the add returns, the newer client edits can be reverted.

The text is gone from the UI, so the next save stores the empty value. Publish then fails with required-field errors on fields the editor already filled in.

Root cause

In packages/ui/src/forms/Form/mergeServerFormState.ts, a server value is accepted when:

incomingField.addedByServer ||
acceptValues === true ||
(acceptValues?.overrideLocalChanges === false && !currentState[path]?.isModified)

Two problems:

  • addedByServer always wins. A field the server adds to a new row is accepted even if the user has already typed into it on the client. The server value is the default from when the request was sent, so it is older than the client's.
  • isModified only protects the field edited last. The UPDATE case in fieldReducer clears isModified on every other field whenever any field's value changes. If the user edits field A, then field B, then a response sent before the A edit returns, A is no longer isModified and gets overwritten with the stale value.

MERGE_SERVER_STATE has no record of what the request sent, so it can't tell "the server changed this" apart from "the client changed this after the request left."

Suggested fix

Pass the form state that was sent with the request into MERGE_SERVER_STATE (both the onChange queue in Form/index.tsx and the form-state request path already build it with deepCopyObjectSimpleWithoutReactComponents). In mergeServerFormState, skip the incoming value for any path whose current client value differs from the value that was sent, unless acceptValues === true (explicit save, which disables the form):

const changedSinceRequest =
  acceptValues !== true &&
  requestState !== undefined &&
  path in currentState &&
  !dequal(currentState[path]?.value, requestState[path]?.value)

let shouldAcceptValue =
  !changedSinceRequest &&
  (incomingField.addedByServer ||
    acceptValues === true ||
    (typeof acceptValues === 'object' &&
      acceptValues !== null &&
      acceptValues.overrideLocalChanges === false &&
      !currentState[path]?.isModified))

// when !shouldAcceptValue && changedSinceRequest: drop only `value`,
// keep the server's `initialValue` and other metadata

We're running this as a patch on @payloadcms/ui 3.93.0. It fixed both cases in our Playwright suite, with no change to explicit Save / Publish behavior. Happy to open a PR if this approach looks right.

Possibly the same root cause as #15719 (autosave reverting the link type in the rich-text link modal).

Link to the code that reproduces this issue

No public repo; the config below is enough on a blank template.

Reproduction Steps

Collection config:

{
  slug: 'pages',
  versions: { drafts: { autosave: { interval: 375 } } },
  fields: [
    { name: 'title', type: 'text', required: true },
    {
      name: 'layout',
      type: 'blocks',
      blocks: [
        {
          slug: 'panels',
          fields: [
            {
              name: 'items',
              type: 'array',
              fields: [
                { name: 'title', type: 'text', required: true },
                { name: 'summary', type: 'textarea', required: true },
                { name: 'image', type: 'upload', relationTo: 'media' },
              ],
            },
          ],
        },
      ],
    },
  ],
}

Case 1:

  1. Create a page, add a panels block, and click Add Item.
  2. Type a Title and Summary into the new item right away.
  3. Under Image, click Create New, upload a file, and save the drawer.
  4. The image is set, but Title (and sometimes Summary) are empty again. Publish reports Items 1 → Title as required.

Uploading first and typing afterward keeps the text.

Case 2:

  1. Type a page Title, then immediately click Add Block → panels (within the 375ms autosave window).
  2. In about one of three tries, the drawer closes and the new row's edits are reverted, or the row is missing from Layout.

Which area(s) are affected?

area: ui

Environment Info

Binaries:
  Node: 26.0.0
  pnpm: 11.2.2
Relevant Packages:
  payload: 3.93.0
  @payloadcms/ui: 3.93.0
  @payloadcms/db-postgres: 3.93.0
  next: 16.3.4
  react: 19.2.8
Operating System:
  Platform: darwin (macOS 26)

Activity

  1. added
    area: ui
    on Nov 20, 2026
  2. self-assigned this
    on Nov 20, 2026
  3. added a commit that references this issue on Nov 23, 2026
    7c766ec
  4. github-actions commented on Nov 24, 2026

    @github-actions
    Contributor

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

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

Metadata

Metadata

Assignees

Labels

BugPayload OSSarea: ui

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions