Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pages-shared): add custom response header when we hit asset preservation cache #7297

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ describe("asset-server handler", () => {
"cache-control": "public, s-maxage=604800",
"x-robots-tag": "noindex",
"cf-cache-status": "HIT", // new header
"x-cf-pgs-apc": "HIT", // new header
});

// Serve with a fresh cache and ensure we don't get a response
Expand Down
9 changes: 8 additions & 1 deletion packages/pages-shared/asset-server/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export const ASSET_PRESERVATION_CACHE_V1 = "assetPreservationCache";
// V2 stores the content hash instead of the asset.
// TODO: Remove V1 once we've fully migrated to V2
export const ASSET_PRESERVATION_CACHE_V2 = "assetPreservationCacheV2";
/**
* Response header will be present w/ "HIT"
* value if served from `ASSET_PRESERVATION_CACHE_V2`
*/
const HEADER_ASSET_PRESERVATION_CACHE_V2 = "x-cf-pgs-apc";
const CACHE_CONTROL_PRESERVATION = "public, s-maxage=604800"; // 1 week

/** The preservation cache should be periodically
Expand Down Expand Up @@ -676,7 +681,9 @@ export async function generateHandler<
const asset = await fetchAsset(assetKey);
if (asset) {
// We know the asset hasn't changed, so use the cached headers.
return new Response(asset.body, preservedResponse);
const response = new Response(asset.body, preservedResponse);
response.headers.set(HEADER_ASSET_PRESERVATION_CACHE_V2, "HIT");
return response;
} else {
logError(
new Error(
Expand Down
Loading