-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml-cache.mjs
More file actions
30 lines (28 loc) · 1.21 KB
/
Copy pathhtml-cache.mjs
File metadata and controls
30 lines (28 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// OpenNext ships BUILD_ID with the assets, so cached HTML and its chunks
// share one build boundary without a new secret or deployment binding.
export async function documentCacheRequest(request, assets) {
if (!assets) return null;
try {
const buildResponse = await assets.fetch(new Request(new URL('/BUILD_ID', request.url)));
if (buildResponse.status !== 200) return null;
const buildId = (await buildResponse.text()).trim();
if (!/^[A-Za-z0-9_-]{1,128}$/.test(buildId)) return null;
const key = new URL(request.url);
key.pathname = `/__rolepatch_html_cache/${buildId}${key.pathname}`;
return new Request(key, request);
} catch {
// Missing/unavailable build metadata must not reuse another build's HTML.
return null;
}
}
export function isDocumentRequest(request) {
return (
!request.headers.has('authorization') &&
!request.headers.has('rsc') &&
!request.headers.has('next-router-prefetch') &&
!request.headers.has('next-router-state-tree') &&
!(request.headers.get('accept') ?? '').includes('text/x-component')
);
}
export const DOCUMENT_CLIENT_CACHE_CONTROL = 'public, max-age=0, must-revalidate';
export const DOCUMENT_EDGE_CACHE_CONTROL = 'public, max-age=86400';