Repository navigation
fix(preview): opt preview and toolbar responses out of the route cache - #1855
Conversation
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 detectedLatest commit: a5dbcb8 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 |
@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: |
There was a problem hiding this comment.
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.
| /** | ||
| * 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; | ||
| } |
There was a problem hiding this comment.
EmDash requires Astro 6, which has the route cache API
There was a problem hiding this comment.
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.
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
routeRulessetup): a valid preview URL (?_preview=<token>) returnedcf-cache-status: MISSwithCache-Control: private, no-storeon request 1 — and shared-cacheHITs 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-Controlheader on preview/toolbar responses. That governs browsers and downstream proxies — but the edge cache follows Astro's route-cache options (on Cloudflare the adapter emitsCloudflare-CDN-Cache-Controlfrom them), which the middleware never touched.The fix:
_previewparam now callscontext.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.Cache-Control: private, no-storeguard 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.context.cacheAPI directly (EmDash requires Astro 6+, which always provides it — thanks @ascorbic). Dev mode and deployments without a cache provider are unaffected:NoopAstroCache/DisabledAstroCacheacceptset(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
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runAI-generated code disclosure
Screenshots / test output
Measurements from the stock demo deploy (before the fix):