Skip to content

Commit 1335c6b

Browse files
aster-voidclaude
andcommitted
fix: improve cache-control headers and add purge logging
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b639270 commit 1335c6b

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/hooks.server.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,14 @@ const handleCache: Handle = async ({ event, resolve }) => {
8181
return response;
8282
}
8383

84-
// Public pages: aggressive Cloudflare caching
85-
// s-maxage=3600: CDN caches for 1 hour
86-
// stale-while-revalidate=86400: serve stale for up to 1 day while revalidating
87-
response.headers.set("Cache-Control", "public, s-maxage=3600, stale-while-revalidate=86400");
84+
// Public pages: layered caching
85+
// max-age=120: browser caches for 2 minutes (snappy navigation)
86+
// s-maxage=3600: CDN caches for 1 hour (purged on mutations)
87+
// stale-while-revalidate=60: serve stale for up to 1 minute while revalidating
88+
response.headers.set(
89+
"Cache-Control",
90+
"public, max-age=120, s-maxage=3600, stale-while-revalidate=60",
91+
);
8892
return response;
8993
};
9094

src/lib/server/services/cloudflare/cache.server.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import { env } from "$lib/env/env.server";
33
const CLOUDFLARE_API_BASE = "https://api.cloudflare.com/client/v4";
44

55
export async function purgeCache(): Promise<void> {
6-
if (!env.CLOUDFLARE_ZONE_ID || !env.CLOUDFLARE_API_TOKEN) return;
6+
if (!env.CLOUDFLARE_ZONE_ID || !env.CLOUDFLARE_API_TOKEN) {
7+
console.warn(
8+
"[cache] Cloudflare cache purge skipped: CLOUDFLARE_ZONE_ID or CLOUDFLARE_API_TOKEN not set",
9+
);
10+
return;
11+
}
712

813
const res = await fetch(`${CLOUDFLARE_API_BASE}/zones/${env.CLOUDFLARE_ZONE_ID}/purge_cache`, {
914
method: "POST",

0 commit comments

Comments
 (0)