Skip to content

fix(blog): stop overlapping autosaves from dropping the penultimate edit - #7147

Merged
guitavano merged 1 commit into
mainfrom
guitavano/autosave-overwrites-edit-loss
Sep 10, 2026
Merged

guitavano merged 1 commit into
mainfrom
guitavano/autosave-overwrites-edit-loss

Conversation

@guitavano

@guitavano guitavano commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes a data-loss bug reported by a content team: while uploading content in the blog editors, editing rapidly (an edit every ~1–2s) would intermittently lose the penultimate edit, which then had to be redone. The workaround was waiting 3–4s between edits.

The block editors autosave with a 700ms debounce, but the write lands in ~2s (network + coalesced commit), so consecutive edits produce overlapping saves. Two concurrency gaps combined to drop an earlier edit:

  1. Writes weren't serialized (use-save-block.ts). Overlapping saves raced their optimistic onMutate/onSuccess writes into the shared decofile cache, and an earlier save finishing last re-applied its older payload over a newer one — permanent loss when responses land out of order. (In cms/FastPreview mode patchDecofile returns only the draft pointer, so the content cache is driven purely by these optimistic writes.)
  2. The useAutosave re-seed guard was too narrow (use-autosave.ts). It was if (!pending) — only the debounce-timer window. During the ~2s save-in-flight window pending is false, so a slow earlier save's echo (older payload landing back in the cache) did setDraft(older), reverting the draft; the user's next edit then built on the reverted draft, omitting the penultimate edit permanently.

Changes

  • Serialize a branch's writes with TanStack v5 scope: { id } in useSaveBlock, so saves run one at a time in edit order — payloads stack and the last write always wins. Backend commits already coalesce per branch, so no correctness cost.
  • Widen the re-seed guard: useAutosave signature (initial, save, delay?) → (initial, save, opts?: { delay?, isSaving? }); re-seed is now if (!pending && !isSaving). All four editors (post/record/category/redirect) pass { isSaving: save.isPending }. isPending stays true across a serialized burst until the last save settles, so the draft can't be transiently reverted mid-burst.

Testing

  • New use-autosave.test.tsx (renderHook) covering the in-flight-echo guard, the pending guard, settled re-seed, and sync.
  • bun test blog suite (213 tests) ✅, bun run check (typecheck) ✅, bun run lint (0 errors) ✅, bun run fmt ✅.

Affected areas

Blog block editors (post, category, author/record, redirect) — the manual content-editing flow. No schema/API changes.

🤖 Generated with Claude Code


Summary by cubic

Fixes the blog editors silently dropping the penultimate edit when typing every 1–2 seconds, caused by overlapping autosaves where a slower earlier save could land after a newer one and overwrite it.

Bug Fixes

  • Serializes a branch's block writes via TanStack scope: { id } in useSaveBlock so saves apply in edit order.
  • Guards useAutosave's re-seed on in-flight saves in addition to the debounce timer, so a slow save's older echo can't revert the draft mid-edit; all four editors pass save.isPending as isSaving.
  • Adds use-autosave.test.tsx covering the in-flight echo, pending, settled re-seed, and sync paths.

Written for commit 5a75b20. Summary will update on new commits.

Review in cubic

Debounced block autosaves (post/record/category/redirect editors) debounce
at 700ms but the write lands in ~2s, so rapid edits overlap. Two concurrency
gaps combined to silently drop an earlier edit:

- Writes weren't serialized: overlapping saves raced their optimistic cache
  writes, and an earlier save finishing last re-applied its older payload over
  a newer one. Serialize per-branch via `scope.id`.
- The `useAutosave` re-seed guard only covered the debounce-timer window, so a
  slow save's older echo reverted the draft mid-flight; the next edit then
  built on the reverted draft, losing the penultimate edit. Guard the re-seed
  on an in-flight save (`isSaving`) too, wired from `save.isPending`.

Adds use-autosave.test.tsx covering the in-flight-echo guard, the pending
guard, settled re-seed, and sync.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions github-actions Bot added the claude PR authored by a coding agent label Sep 10, 2026
@guitavano
guitavano merged commit b7c1caa into main Sep 10, 2026
34 checks passed
@guitavano
guitavano deleted the guitavano/autosave-overwrites-edit-loss branch September 10, 2026 20:47
decocms Bot pushed a commit that referenced this pull request Sep 10, 2026
PR: #7147 fix(blog): stop overlapping autosaves from dropping the penultimate edit
Bump type: patch

- decocms (apps/api/package.json): 4.351.1 -> 4.351.2
- @decocms/native (apps/native/package.json): 4.351.1 -> 4.351.2

Deploy-Scope: web
aka-sacci-ccr pushed a commit that referenced this pull request Sep 11, 2026
One conflict, in the post editor, and it was the interesting kind: both
sides had rewritten the same header.

The restructure moved the header block below a floating expand/close
cluster; upstream changed what's inside it. Kept the restructure's layout
and applied upstream's two behaviour changes into it, because taking
either side whole would have silently dropped a fix:

- #7152 ungated "See preview" from `hasErrors`. The restructure's copy
  still had `disabled={!previewUrl || hasErrors}`, which is the bug that
  PR fixes, so the gate is gone and the title no longer branches on it.
- #7147 gave `useAutosave` an `isSaving` option so overlapping autosaves
  stop dropping the penultimate edit. Threaded through, keeping
  `buildPostBlock` and the three-value destructure the restructure needs
  for `syncPost` on a block rename.

#7150's preview-route fix needed nothing here: the call site is identical
on both sides and the change lives in `buildBlogPostPreviewUrl`.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

claude PR authored by a coding agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant