Skip to content

fix(preview): opt preview and toolbar responses out of the route cache - #1855

Merged
ascorbic merged 3 commits into
emdash-cms:mainfrom
swissky:fix/preview-route-cache-optout
Jul 9, 2026
Merged

ascorbic merged 3 commits into
emdash-cms:mainfrom
swissky:fix/preview-route-cache-optout

Conversation

@swissky

@swissky swissky commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

With route caching enabled (Workers Cache on Cloudflare), preview responses and editor-toolbar HTML end up in the shared edge cache and get served to subsequent requests without any token verification or worker invocation.

Verified empirically on a stock Cloudflare deploy (0.27.0, the documented routeRules setup): a valid preview URL (?_preview=<token>) returned cf-cache-status: MISS with Cache-Control: private, no-store on request 1 — and shared-cache HITs on requests 2 and 3. The draft content stays reachable until TTL/purge, including after the token expires. (First reported in the #1820 review thread.)

Root cause: the request-context middleware sets only the Cache-Control header on preview/toolbar responses. That governs browsers and downstream proxies — but the edge cache follows Astro's route-cache options (on Cloudflare the adapter emits Cloudflare-CDN-Cache-Control from them), which the middleware never touched.

The fix:

  • Any request carrying a _preview param now calls context.cache.set(false) (opt out of route caching). This is deliberately keyed on the param's presence rather than token validity: preview URLs are per-token, so cached copies are useless at best and drafts at worst.
  • Toolbar-injected editor responses opt out the same way (their Cache-Control: private, no-store guard from Editor toolbar injection doesn't set Cache-Control: private, no-store, poisoning shared CDN caches #1398 had the same blind spot). Non-HTML editor responses are unaffected and stay cacheable.
  • The middleware uses Astro's typed context.cache API directly (EmDash requires Astro 6+, which always provides it — thanks @ascorbic). Dev mode and deployments without a cache provider are unaffected: NoopAstroCache/DisabledAstroCache accept set(false) as a no-op.

New unit tests cover: preview param present → opt-out; toolbar injected → opt-out; editor non-HTML response → cache untouched; anonymous fast path → cache untouched.

Type of change

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

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (n/a — no admin UI changes)
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion: (n/a — bug fix)

AI-generated code disclosure

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

Screenshots / test output

Measurements from the stock demo deploy (before the fix):

request 1: cf-cache-status: MISS   cache-control: private, no-store
request 2: cf-cache-status: HIT
request 3: cf-cache-status: HIT
✓ tests/unit/astro/request-context-route-cache.test.ts (5 tests)
✓ tests/unit/astro/ (133 tests total)

Cache-Control: private, no-store only governs browsers and downstream
proxies. With route caching enabled, the shared edge cache (Workers
Cache) follows the route-cache options instead, so a valid preview
response was stored in the shared cache and served on hits without any
token verification — draft content stayed reachable until TTL/purge,
even after token expiry. Toolbar-injected editor HTML had the same gap.

Verified empirically on a stock Cloudflare deploy: preview URL request
1 was MISS with private, no-store, requests 2+ were shared-cache HITs.

The middleware now calls context.cache.set(false) for any request
carrying a _preview param and whenever the editor toolbar is injected.
Read defensively so Astro versions without route caching are unaffected.
@changeset-bot

changeset-bot Bot commented Jul 7, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a5dbcb8

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

This PR includes changesets to release 16 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@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/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash Patch
@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 added area/core size/M review/needs-review No maintainer or bot review yet labels Jul 7, 2026
@pkg-pr-new

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

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: a5dbcb8

@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 is the right fix for the right problem. Preview URLs and editor-toolbar responses are session-specific, and relying only on Cache-Control left them exposed to Astro's route-level edge cache. Calling context.cache.set(false) in those cases is the idiomatic Astro opt-out and fits the EmDash middleware architecture without breaking older Astro versions or non-cache deployments.

I checked the diff, the full middleware source, the new tests, and the changeset. The fix correctly opts out any request carrying _preview (valid or invalid token) as well as HTML responses that receive the toolbar, while leaving non-HTML editor responses and anonymous fast-path requests unchanged. The context.cache read is defensive (?.set(false)), so older Astro and dev mode are unaffected.

No AGENTS.md convention violations stood out: the changeset targets the right published package (emdash), uses a present-tense user-facing description, and the tests cover the key cases. Looks clean to me.

@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Jul 7, 2026
Comment on lines +21 to +29
/**
* The subset of Astro's route-cache API the middleware needs. `context.cache`
* ships with Astro's route caching; typed structurally (and called
* optionally) so the middleware stays compatible with Astro versions that
* predate it.
*/
interface RouteCacheLike {
set(input: false): void;
}

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.

EmDash requires Astro 6, which has the route cache API

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.

Good point, thanks — dropped the structural interface and the defensive read in a5dbcb8. The middleware now uses context.cache directly (typed via APIContext["cache"]), which also let me remove the type-assertion lint suppression and the "absent cache" test.

EmDash requires Astro 6+, which always provides context.cache, so the
structural RouteCacheLike interface, the type assertion, and the
optional call were unnecessary hedging. Type the helper against
APIContext["cache"] and drop the "absent cache" test.
@github-actions github-actions Bot added review/needs-rereview New commits since the last review and removed review/approved Approved; no new commits since labels Jul 7, 2026

@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 450ea81 into emdash-cms:main Jul 9, 2026
51 checks passed
@emdashbot emdashbot Bot mentioned this pull request Jul 9, 2026
@swissky
swissky deleted the fix/preview-route-cache-optout branch September 10, 2026 06:11
@swissky swissky mentioned this pull request Sep 10, 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/core review/needs-rereview New commits since the last review size/M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants