Skip to content

Save a document as it is edited - #3

Merged
afternoon merged 3 commits into
mainfrom
claude/named-files-auto-save-x3k4vf
Aug 31, 2026
Merged

afternoon merged 3 commits into
mainfrom
claude/named-files-auto-save-x3k4vf

Conversation

@afternoon

@afternoon afternoon commented Aug 31, 2026 •

Copy link
Copy Markdown
Owner

A named document — one that exists at /d/:id, for a signed-in user — now writes itself once the typing pauses. Save becomes a way to store the work now, not the only way it is ever stored.

Unsaved work is deliberately untouched. A draft has no id to write to and no owner to write it for, so / keeps its localStorage draft and its explicit Save, and signing in is still what turns a draft into a document.

Pacing, and why the preview moved

Auto-save forced the two halves of a save apart. Content is a database row, so it is written on every pause (1.5s). The preview image behind it is a Browser Run session against a quota shared with the PDF output — a capture per pause in typing would spend the quota the document's own output depends on.

So:

  • PUT /api/documents/:id takes a capturePreview flag. Absent means yes, so an explicit save and document creation behave exactly as before; auto-save writes quietly.
  • POST /api/documents/:id/thumbnail is new: it renders the stored document (never content from the request, which would let a caller put any image on a card) and does nothing when the current revision already has a capture, so a repeated request costs no browser time.
  • The editor asks for that capture once the editing has actually stopped — 15s of quiet, and only while the editor matches what is stored, since a preview of content about to be overwritten is already wrong.

Between a quiet write and the capture that follows it, the card shows "No preview" — the same honest placeholder it already shows for a failed or missing capture.

Hook changes

useDocumentSave now takes the live content instead of being handed it per call, which is what lets it act on its own. stateFor(doc) collapses into state. An existing document starts out already saved, so opening one is not mistaken for unwritten work — without that, every open would cost a write and clear the card's preview.

A write in flight is never overlapped: when it lands, savedContent changes and the effect re-runs against the content as it is then, so anything typed during the request is picked up rather than written from a stale closure. A failed write retries on the next edit rather than immediately, and the button reads "Retry save" in the meantime.

Merged with main

File drops landed on main and touched the same line in App.tsx (the editor holds a toast beside the save hook); both are kept. Drop's own note that "the save is still the user's to make" is no longer true of a stored document, so it now says what stays true — the undo history is the way back, and undoing is written back the same way.

Testing

bun run test (194 passing), tsc --noEmit, biome check and bun run build all run clean on the merged tree.

  • 16 new tests: auto-save timing, the draft and signed-out cases, opening a document, an edit made mid-flight, retry after failure, preview pacing, and the API client's request/response shapes.
  • src/server/documents.test.ts still fails to load — node:sqlite cannot be bundled under this vite environment. Verified identical on main in a clean worktree; untouched by this branch.

Not covered: closing the tab inside the 1.5s debounce still loses that last edit. Flushing on pagehide is a separate, self-contained change.

A document that exists now writes itself once the typing pauses, for a
signed-in user. Losing an afternoon's work because nobody pressed a button
is not a state a document with a name and a home should be able to reach,
and Save becomes a way to store the work *now* rather than the only way it
is ever stored.

Unsaved work is deliberately untouched: a draft has no id to write to and
no owner to write it for, so `/` keeps its localStorage draft and its
explicit Save, and signing in is still what turns one into the other.

The two halves of a save had to be separated to do this. Content is a
database row, so it is written on every pause; the preview behind it is a
browser render against a quota shared with the PDF output, so a capture per
pause would spend the quota the document's own output depends on. Saving
now takes a `capturePreview` flag — absent means yes, so an explicit save
behaves exactly as before — and the editor asks for the image separately,
once the editing has actually stopped, through a new POST on the thumbnail
route. That endpoint renders the *stored* document rather than content from
the request, and does nothing when the current revision has already been
captured, so a repeated request costs no browser time.

The hook takes the live content rather than being handed it per call, which
is what lets it act on its own; `stateFor(doc)` collapses into `state` now
that it knows what is on screen. An existing document starts out already
saved, so opening one no longer looks like unwritten work — without that,
every open would cost a write and clear the card's preview.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MTqa7LTmuJMFEmNohAka5n
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
htmlcsspdf 9378806 Commit Preview URL

Branch Preview URL
Aug 31 2026, 08:55 PM

claude and others added 2 commits August 31, 2026 20:52
File drops landed on main and touched the same line in App.tsx: the editor
now holds a toast alongside the save hook, which takes the live content.
Both are kept.

Drop's own note that "the save is still the user's to make" is no longer
true of a stored document — dropping a file into one is written like any
other edit — so it now says what stays true: the undo history is the way
back, and undoing is written back the same way.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MTqa7LTmuJMFEmNohAka5n
@afternoon
afternoon marked this pull request as ready for review August 31, 2026 20:55
@afternoon
afternoon merged commit 45cfe2e into main Aug 31, 2026
1 of 2 checks passed
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.

2 participants