NGU-Web/docs/drafts.md
Aidan Dona a6c66d4607 Drafts of every record, previewed on the real page
Migration 077 keeps one draft per record of any entity. Saving a draft
checks it by applying it in a transaction that is rolled back; Preview
opens the record's page in a tab that sends X-Draft-Preview, and the
server answers those reads from a second connection holding the applied
draft, then rolls back, so nobody else sees it. Publish is the ordinary
save, which drops the draft. tx() now nests as a savepoint.

Also commits TODO.md: the answered questions, and parts 1 to 3 moved to
Done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 18:21:53 -05:00

2.1 KiB

Drafts

Any record the admin edits (events, people, organizations, blog posts, the front page, the site pages, Navigation, Site settings...) can be saved as a draft instead of going live (migration 077). One draft per record; anyone who can edit the record can save, preview, publish or discard it.

In the editor

  • Save draft keeps the form as the record's draft. It's checked the way a save is (same field errors), but nothing on the site changes.
  • Preview saves the draft and opens the record's page in a new tab showing it, with an amber bar ("Previewing a draft"). Only a signed-in admin account (viewer and up) sees the draft; anyone else opening that address sees the live page. Stop previewing in the bar returns the tab to the live site.
  • Publish (the Save button, while there's a draft) saves it for real and drops the draft.
  • Discard draft throws it away; the live version is untouched.

Opening a record that has a draft opens the draft, with a note saying who saved it and when. If the live record was saved after the draft was started, the note says so: publishing replaces those changes. Lists mark records with a draft.

Records not yet created have no drafts: Create is the only save (leave them unpublished until they're ready).

How it works

server/src/drafts.js. A draft is the editor's whole form, as a save would send it. Saving one applies it inside a transaction that is always rolled back (rolledBackSync in server/src/db.js), so it's validated without being written. A preview is a public API read sent with X-Draft-Preview: <entity>/<id> (src/lib/api.ts adds it in a tab opened with ?preview=): the server opens a second connection to the database, applies the draft in a transaction there, answers the read from it, and rolls back. WAL keeps those writes unseen by every other request. Public reads carry Vary: X-Draft-Preview, so a browser never mixes a cached live answer with a preview.

tx() nests as a savepoint, which is what lets a save run inside the rolled-back transaction. Where each entity's page is: PREVIEW_PATHS in src/lib/adminSchema.ts (an entity without one has drafts but no Preview button).