Skip to content

feat: toolbar config option with client-side bootstrap mode for shared caches - #1886

Merged
ascorbic merged 3 commits into
emdash-cms:mainfrom
swissky:feat/toolbar-client-bootstrap
Jul 10, 2026
Merged

ascorbic merged 3 commits into
emdash-cms:mainfrom
swissky:feat/toolbar-client-bootstrap

Conversation

@swissky

@swissky swissky commented Jul 9, 2026 •

Copy link
Copy Markdown
Member

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 toolbar config option on emdash():

  • "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 _edit query param, which is always rendered fresh with the full server toolbar and Cache-Control: private, no-store. Non-editors opening a shared ?_edit URL 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-mode cookie so the user isn't stranded in edit mode with no UI.

Preview and edit-mode responses keep rendering server-side with private, no-store in every mode, exactly as today.

Verification

  • 10 new unit tests covering all three modes: anonymous/editor variants byte-identical in client mode, _edit render is no-store, canonical redirect for anonymous and low-role users, stale edit cookie handling, false mode, and a syntax check of the generated bootstrap script.
  • Verified end to end on the simple demo (Node adapter, dev): anonymous HTML carries only the bootstrap and no cache-header changes; editor HTML without the param is byte-identical to anonymous; the pill appears after admin login, clicking it lands on ?_edit=1 with 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; ?_edit as anonymous 302s to the canonical URL preserving other params.

Notes

  • The bootstrap is inline <script> like the existing toolbar; the docs note the CSP hash requirement for strict policies.
  • The localStorage key literals are duplicated between core and the admin package (admin can't import core); both sides carry a pointer comment.
  • Docs: new toolbar section in the configuration reference, including the caveat that user templates branching on Astro.locals.user still fragment the cache on their own.
  • The toolbar itself is public-site UI (plain HTML string, not the Lingui'd admin SPA), so the two new strings ("Edit", "Hide toolbar") are English like all existing toolbar strings.

Closes the implementation side of Discussion #1742.

Type of change

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Cursor + Fable 5

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)

@changeset-bot

changeset-bot Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f14502d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 16 packages
Name Type
emdash Minor
@emdash-cms/admin Minor
@emdash-cms/cloudflare Minor
@emdash-cms/sandbox-workerd Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/auth Minor
@emdash-cms/blocks Minor
@emdash-cms/gutenberg-to-portable-text Minor
@emdash-cms/x402 Minor
create-emdash Minor
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

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

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Scope check

This 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.
@ascorbic

ascorbic commented Jul 9, 2026

Copy link
Copy Markdown
Member

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.

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +89 to 96
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;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).
@swissky
swissky force-pushed the feat/toolbar-client-bootstrap branch from 060f55c to 8452314 Compare July 9, 2026 06:56
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed needs-rebase review/needs-review No maintainer or bot review yet labels Jul 9, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 9, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@1886

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@1886

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@1886

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@1886

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@1886

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@1886

emdash

npm i https://pkg.pr.new/emdash@1886

create-emdash

npm i https://pkg.pr.new/create-emdash@1886

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@1886

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@1886

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@1886

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@1886

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@1886

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@1886

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@1886

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@1886

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@1886

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@1886

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@1886

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@1886

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@1886

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@1886

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@1886

commit: f14502d

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.

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@ascorbic
ascorbic merged commit 60811c0 into emdash-cms:main Jul 10, 2026
48 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jul 10, 2026
@swissky
swissky deleted the feat/toolbar-client-bootstrap branch September 10, 2026 06:11
@danielmlr danielmlr mentioned this pull request Oct 5, 2026
5 of 22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/admin area/core area/docs bot:review Trigger an emdashbot code review on this PR cla: signed review/needs-rereview New commits since the last review size/XL

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants