Skip to content

fix(ui): prevent autosave from reverting recently edited fields - #17697

Open
lucasvass1 wants to merge 1 commit into
payloadcms:mainfrom
lucasvass1:fix/autosave-overwrites-recent-edits
Open

fix(ui): prevent autosave from reverting recently edited fields#17697
lucasvass1 wants to merge 1 commit into
payloadcms:mainfrom
lucasvass1:fix/autosave-overwrites-recent-edits

Conversation

@lucasvass1

Copy link
Copy Markdown

Fixes #17676

Summary

  • Autosave could overwrite recently typed field values: switching to a different field while a previous autosave request was still in flight cleared that field's "modified" protection, so the stale response (with an older/partial value) could land and overwrite the newer edit. Fixed by tracking a per-field modifiedAt timestamp and comparing it against when each request was sent — a response is only accepted for a field if no local edit happened to it after that specific request was dispatched.
  • Media library (and any relationship/upload) list drawer would flicker/reload during editing: unmodified object/array-valued fields (relationship, upload) got a brand new, content-identical value reference on every accepted autosave merge, which cascaded into useListDrawer recomputing and remounting the open drawer. Fixed by preserving the existing reference in mergeServerFormState when the incoming value is deeply equal to the current one.

Test plan

  • Added an e2e regression test reproducing the reported race (type in one field, switch to another before autosave completes) - fails without the fix, passes with it
  • Added an e2e test asserting an open relationship list drawer isn't remounted by an unrelated autosave - fails without the fix, passes with it
  • Added a unit test in mergeServerFormState covering reference preservation for content-identical values
  • Existing autosave/computed-field tests in test/form-state still pass (no regression to the "don't overwrite a field being actively edited" behavior)
  • pnpm run build:core and lint pass

… remounting relationship/upload list drawers

Switching to a different field while a previous autosave request was still
in flight cleared that field's modified protection, so a stale response
could overwrite the newer local edit. Track a per-field last-modified
timestamp and compare it against when each request was sent, so a response
is only accepted if no local edit happened after it was dispatched.

Also preserve the value/initialValue reference for unmodified fields when
the incoming server value is content-identical, since object/array-shaped
values (e.g. relationship and upload fields) otherwise get a new reference
on every autosave merge, causing their list drawers to needlessly recompute
filter options and remount.
@jacobsfletch

Copy link
Copy Markdown
Member

@lucasvass1 I think this may already be fixed by #17455. Would you mind testing that PR to confirm?

@lucasvass1

Copy link
Copy Markdown
Author

@jacobsfletch oi, tudo bem? Analisei a PR #17455 e ela corrige um dos erros que corrigi, porém não o segundo.

Ela resolve a sobrescrita de campo por resposta de autosave obsoleta (digitar em um campo, trocar pra outro antes do autosave anterior terminar, e o valor reverter).

Mas não resolve a media library recarregando/piscando durante o autosave. Isso acontece porque, mesmo quando um campo relationship/upload não foi editado, o merge da PR aceita o valor vindo do servidor como se fosse novo — e como esse valor chega recém-serializado a cada resposta, ele tem uma referência diferente mesmo com o mesmo conteúdo. Isso faz o drawer da lista recalcular e remontar sem necessidade.

Testei isso na prática, rodando meus testes contra o branch da #17455: o teste da sobrescrita passa, mas o teste do remount do drawer falha.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autosave can overwrite recently typed field values and reload the media library

2 participants