Skip to content

Commit 24652f5

Browse files
igoramfclaude
andcommitted
feat(runtime): switch page cache to opt-in via PAGE_CACHE_ALLOWED_KEY
Replaces the opt-out model (cache unless PAGE_DIRTY_KEY is set) with an opt-in model (cache only if PAGE_CACHE_ALLOWED_KEY is set). Apps that do not explicitly opt in will no longer receive CDN Cache-Control headers from the runtime, preserving their existing behavior. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 56a36b7 commit 24652f5

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

runtime/middleware.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// deno-lint-ignore-file no-explicit-any
22
import { HTTPException } from "@hono/hono/http-exception";
33
import { DECO_MATCHER_HEADER_QS } from "../blocks/matcher.ts";
4-
import { PAGE_DIRTY_KEY } from "../blocks/utils.tsx";
4+
import { PAGE_CACHE_ALLOWED_KEY } from "../blocks/utils.tsx";
55
import { Context, context } from "../deco.ts";
66
import {
77
type Exception,
@@ -106,7 +106,6 @@ async (ctx, next) => {
106106

107107
const DEBUG_COOKIE = "deco_debug";
108108
const DEBUG_ENABLED = "enabled";
109-
const PAGE_CACHE_ENABLED = Deno.env.get("DECO_PAGE_CACHE_ENABLED") === "true";
110109
const PAGE_CACHE_CONTROL = Deno.env.get("DECO_PAGE_CACHE_CONTROL") ??
111110
"public, max-age=90, s-maxage=90, stale-while-revalidate=3600, stale-if-error=86400";
112111

@@ -430,12 +429,12 @@ export const middlewareFor = <TAppManifest extends AppManifest = AppManifest>(
430429
const hasSetCookie = getSetCookies(newHeaders).length > 0;
431430
const contentType = newHeaders.get("Content-Type") ?? "";
432431
const isHtmlResponse = contentType.includes("text/html");
433-
const isPageDirty = ctx.var.bag?.has(PAGE_DIRTY_KEY);
432+
const isPageCacheAllowed = ctx.var.bag?.has(PAGE_CACHE_ALLOWED_KEY);
434433

435434
if (hasSetCookie) {
436435
// Set-cookie present: never cache (same behavior as main)
437436
newHeaders.set("Cache-Control", "no-store, no-cache, must-revalidate");
438-
} else if (isHtmlResponse && PAGE_CACHE_ENABLED && !isPageDirty) {
437+
} else if (isHtmlResponse && isPageCacheAllowed) {
439438
const flags = ctx.var?.flags ?? [];
440439
const allFlagsCacheable = flags.length > 0
441440
? flags.every((flag) => flag.cacheable === true)

0 commit comments

Comments
 (0)