Repository navigation
feat: toolbar config option with client-side bootstrap mode for shared caches - #1886
Conversation
🦋 Changeset detectedLatest commit: f14502d The changes in this PR will be included in the next version bump. This PR includes changesets to release 16 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Scope checkThis PR changes 543 lines across 11 files. Large PRs are harder to review and more likely to be closed without review. If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs. See CONTRIBUTING.md for contribution guidelines. |
…aches Implements the design agreed in Discussion emdash-cms#1742: a `toolbar` option with "server" (default, unchanged), "client" (cache-identical public HTML with a client-rendered Edit pill and an `_edit` query param for fresh editor renders), and `false` (disabled). The toolbar is now also dismissible in the browser.
|
Thanks for this. We've been running into this with the CF blog, so I'm going to ask the team to test the build preview to see if it solves it before merging. |
There was a problem hiding this comment.
This PR implements the approved Discussion #1742 feature for a cache-safe editor toolbar and is architecturally sound. The three toolbar modes are cleanly separated, public HTML stays byte-identical in client mode, the _edit param is gated server-side by session/role, and the bootstrap verifies the real session before entering edit mode. Tests, docs, and a changeset are all present.
One real shared-cache correctness gap remains: the 302 redirect sent to non-editors who open a shared ?_edit URL does not carry Cache-Control: private, no-store. Edge caches that cache 302 responses (or are configured aggressively) could serve that redirect to a later editor request that hits the same URL, making the _edit path unreachable. The redirect should be marked uncacheable and the test should assert it.
| function redirectToCanonical(url: URL): Response { | ||
| const canonical = new URL(url); | ||
| canonical.searchParams.delete(EDIT_PARAM); | ||
| return new Response(null, { | ||
| status: 302, | ||
| headers: { Location: canonical.pathname + canonical.search + canonical.hash }, | ||
| }); | ||
| // Toolbar-injected HTML is session-specific (its presence reveals an active | ||
| // editor session); it must never be stored in a shared CDN cache and served | ||
| // to anonymous visitors. Mirrors the preview branch's guard (#1398). | ||
| result.headers.set("Cache-Control", "private, no-store"); | ||
| return result; | ||
| } |
There was a problem hiding this comment.
[needs fixing] The redirectToCanonical response does not set Cache-Control, so a shared cache could store the 302 it returns for non-editors who open ?_edit. If a later editor request hits the same cached URL, the cache could serve the redirect instead of routing them to the fresh toolbar render, breaking the client-mode edit path.
Set private, no-store on the redirect so shared caches never keep it:
function redirectToCanonical(url: URL): Response {
const canonical = new URL(url);
canonical.searchParams.delete(EDIT_PARAM);
return new Response(null, {
status: 302,
headers: {
Location: canonical.pathname + canonical.search + canonical.hash,
"Cache-Control": "private, no-store",
},
});
}Please also add an assertion in request-context-toolbar-mode.test.ts that the redirect response carries Cache-Control: private, no-store.
There was a problem hiding this comment.
Fixed in 8452314 — the redirect now carries Cache-Control: private, no-store, and both the redirect and the _edit editor render additionally opt out of the route cache (the Workers Cache ignores Cache-Control, so the header alone wouldn't have covered it there). Tests assert both the header and the route-cache opt-out. Also rebased onto main to pick up the #1855 route-cache changes.
Addresses review: the 302 for non-editors now carries Cache-Control: private, no-store, and both the redirect and the editor render opt out of the route cache (a cached redirect would bounce editors back to the canonical URL).
060f55c to
8452314
Compare
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
CodeQL flags the <script>…</script> regex as a bad HTML-filtering pattern (case-sensitive tags). Use indexOf/lastIndexOf — we are slicing our own generated string, not filtering HTML.
What does this PR do?
Implements the design agreed in Discussion #1742 (labeled Approved for PR): a reliable editor toolbar for sites whose public HTML is served through a shared cache (Cloudflare Cache Everything / Workers Cache, Fastly, Varnish, …), where the server-injected toolbar today appears and disappears with cache state.
New
toolbarconfig option onemdash():"server"(default) — current behavior, unchanged."client"— public HTML is byte-identical for every visitor (nothing session-specific is injected, cache headers untouched, so shared caches serve one entry for everyone). A tiny bootstrap script renders an "Edit" pill for browsers that have logged into the admin, based on a non-secret localStorage flag set by the admin SPA on login (covers passkey, OAuth, magic link, dev bypass) and cleared on logout. Clicking the pill verifies the real session (GET /_emdash/api/auth/me) — logged out goes to the login page — and reloads the page with an_editquery param, which is always rendered fresh with the full server toolbar andCache-Control: private, no-store. Non-editors opening a shared?_editURL get an early 302 redirect to the canonical URL, so the param can't leak drafts or prime extra cache entries with page content. The param deliberately does not persist across navigation.false— never render the toolbar or bootstrap.Additionally (all modes): the toolbar now has a × dismiss button (per-browser localStorage flag, cleared the next time an editor opens the admin). Dismissing while edit mode is active also clears the
emdash-edit-modecookie so the user isn't stranded in edit mode with no UI.Preview and edit-mode responses keep rendering server-side with
private, no-storein every mode, exactly as today.Verification
_editrender isno-store, canonical redirect for anonymous and low-role users, stale edit cookie handling,falsemode, and a syntax check of the generated bootstrap script.?_edit=1with the full toolbar (private, no-store); edit-mode toggle works from there; dismiss hides the pill/toolbar, clears the edit cookie, and un-dismisses on the next admin visit;?_editas anonymous 302s to the canonical URL preserving other params.Notes
<script>like the existing toolbar; the docs note the CSP hash requirement for strict policies.toolbarsection in the configuration reference, including the caveat that user templates branching onAstro.locals.userstill fragment the cache on their own.Closes the implementation side of Discussion #1742.
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain.AI-generated code disclosure
Screenshots / test output
toolbar: "client", editor after clicking the Edit pill (page reloaded with?_edit=1, full toolbar with the new × dismiss button):(see Verification above — 10/10 unit tests pass, live-tested on the simple demo)