Skip to content

Commit 8bb9b8c

Browse files
committed
Add support for fingerprinted static asset cache policy improvements
1 parent 8d6bcf5 commit 8bb9b8c

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/server.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ interface ManifestLookupOptions {
3131
branchOrRef: string;
3232
}
3333

34+
const FingerprintedExtensions = new Set(['.css', '.js', '.svg']);
35+
3436
export const getManifest = async (siteId: string, branchOrRef: string) => {
3537
const keys = [
3638
datastore.key(['Fileset2Manifest', `${siteId}:branch:${branchOrRef}`]),
@@ -301,6 +303,11 @@ export function createApp(siteId: string) {
301303
urlPath += 'index.html';
302304
}
303305

306+
// Used for cache-control settings.
307+
const ext = fsPath.extname(req.path.split('?')[0]);
308+
const isFingerprinted =
309+
FingerprintedExtensions.has(ext) && req.url.includes('fingerprint=');
310+
304311
// Handle static content.
305312
const manifestPaths = manifest.paths;
306313
let blobKey =
@@ -382,7 +389,9 @@ export function createApp(siteId: string) {
382389
delete proxyRes.headers['x-goog-stored-content-length'];
383390
delete proxyRes.headers['x-guploader-response-body-transformations'];
384391
delete proxyRes.headers['x-guploader-uploadid'];
385-
if (isLive && !is404Page) {
392+
if (isFingerprinted) {
393+
proxyRes.headers['cache-control'] = 'public, max-age=31536000';
394+
} else if (isLive && !is404Page) {
386395
// This cannot be "private, max-age=0" as this kills perf.
387396
// This also can't be a very small value, as it kills perf. 0036 seems to work correctly.
388397
// The padded "0036" keeps the Content-Length identical with `3600`.

0 commit comments

Comments
 (0)