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

Aperta
#19,797 0 commenti 0 reazioni 1 assegnatario Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@jacobsfletch ci sta già lavorando.

Dal 20/11/2026.

  • #17455 di @jacobsfletch — aperta

Valutazione

Questa issue non è ancora stata valutata.

Descrizione

area: ui Bug Payload OSS
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)
Lingua principale
TypeScript
Stelle
45.8k
Fork
4.4k
Merge medio
3g 4h
PR unite (30g)
210

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di payloadcms/payload

Tutte le issue di payloadcms/payload

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.