Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/cloudflare/src/cache/cdn-adapter.runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,20 @@ import {
} from "vinext/shims/cdn-cache";
import type { CacheHandlerValue, IncrementalCacheValue } from "vinext/shims/cache";
import { getRequestExecutionContext } from "vinext/shims/request-context";
import { VINEXT_CDN_BUILD_ID_HEADER } from "./cdn-build-id.js";

const CACHEABLE_EDGE_DIRECTIVE_RE = /(?:^|,)\s*(?:s-maxage|max-age)\s*=/i;
const EDGE_POLICY_HEADERS = ["CDN-Cache-Control", "Cloudflare-CDN-Cache-Control"] as const;

function getBuildIdentityResponseHeader(): CdnResponseHeaders {
const buildId = process.env.__VINEXT_BUILD_ID;
return buildId ? { [VINEXT_CDN_BUILD_ID_HEADER]: buildId } : {};
}

/** Remove every response header whose cache semantics are owned by Cloudflare. */
function clearCloudflareCdnResponseHeaders(cacheControl: string): CdnResponseHeaders {
return {
...getBuildIdentityResponseHeader(),
"Cache-Control": cacheControl,
"CDN-Cache-Control": null,
"Cloudflare-CDN-Cache-Control": null,
Expand Down Expand Up @@ -172,6 +179,10 @@ export class CloudflareCdnCacheAdapter implements CdnCacheAdapter {
// intentionally empty
}

buildResponseIdentityHeaders(): CdnResponseHeaders {
return getBuildIdentityResponseHeader();
}

buildResponseHeaders(input: CdnCacheableHeaderInput): CdnResponseHeaders {
// No cacheable policy → nobody stores it.
if (!input.cacheControl) {
Expand All @@ -188,6 +199,7 @@ export class CloudflareCdnCacheAdapter implements CdnCacheAdapter {
// SWR policy on CDN-Cache-Control (edge caches + revalidates); the browser
// is told to revalidate every reuse so it never serves a stale stored copy.
const headers: CdnResponseHeaders = {
...getBuildIdentityResponseHeader(),
"Cache-Control": BROWSER_REVALIDATE,
"CDN-Cache-Control": toEdgeCacheControl(input.cacheControl),
"Cloudflare-CDN-Cache-Control": null,
Expand Down
1 change: 1 addition & 0 deletions packages/cloudflare/src/cache/cdn-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function cdnAdapter(options?: Record<string, never>) {
adapter: fileURLToPath(import.meta.resolve("./cdn-adapter.runtime.js")),
options,
capabilities: {
buildIdentity: "response-header" as const,
responseVary: "verbatim" as const,
},
};
Expand Down
2 changes: 2 additions & 0 deletions packages/cloudflare/src/cache/cdn-build-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** Build identity stamped by the Cloudflare CDN adapter on page responses. */
export const VINEXT_CDN_BUILD_ID_HEADER = "X-Vinext-Build-Id";
Loading
Loading