-
Notifications
You must be signed in to change notification settings - Fork 19
feat: tiered cache headers #958
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| export const CACHE_HEADERS = { | ||
| success: 'public, max-age=3600, s-maxage=14400', | ||
| immutable: 'public, immutable, max-age=31536000, s-maxage=31536000', | ||
| mutable: 'public, max-age=86400, s-maxage=86400, must-revalidate', | ||
| failure: 'private, no-cache, no-store, max-age=0, must-revalidate', | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,15 +2,16 @@ import * as Sentry from '@sentry/cloudflare'; | |
| import { CACHE_HEADERS } from '../constants/cache'; | ||
| import docsDirectory from '../constants/docsDirectory.json' assert { type: 'json' }; | ||
| import type { Context } from '../context'; | ||
| import type { GetFileResult } from '../providers/provider'; | ||
| import type { GetFileResult, HeadFileResult } from '../providers/provider'; | ||
| import { R2Provider } from '../providers/r2Provider'; | ||
| import responses from '../responses'; | ||
| import { hasTrailingSlash, isDirectoryPath } from '../utils/path'; | ||
| import { cacheControlFor } from '../utils/cache'; | ||
| import type { Middleware } from './middleware'; | ||
| import latestVersions from '../constants/latestVersions.json' assert { type: 'json' }; | ||
| import type { Request } from '../routes/request'; | ||
| import { renderDirectoryListing } from '../utils/directoryListing'; | ||
| import { parseConditionalHeaders } from '../utils/request'; | ||
| import { getOriginalUrl, parseConditionalHeaders } from '../utils/request'; | ||
| import { once } from '../utils/memo'; | ||
|
|
||
| const getProvider = once((ctx: Context) => new R2Provider({ ctx })); | ||
|
|
@@ -41,7 +42,7 @@ async function handleDirectory( | |
| ): Promise<Response> { | ||
| if (!hasTrailingSlash(request.urlObj.pathname)) { | ||
| // We always want directory listing requests to have a trailing slash | ||
| const url = request.unsubstitutedUrl ?? request.urlObj; | ||
| const url = getOriginalUrl(request); | ||
| return Response.redirect(`${url}/`, 301); | ||
| } | ||
|
flakey5 marked this conversation as resolved.
|
||
|
|
||
|
|
@@ -58,21 +59,29 @@ async function handleDirectory( | |
|
|
||
| let responseBody; | ||
| if (request.method === 'GET') { | ||
| responseBody = renderDirectoryListing( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Directory index mutable cacheLow Severity When a directory request is satisfied by serving Additional Locations (1)Reviewed by Cursor Bugbot for commit 82a6785. Configure here. |
||
| request.unsubstitutedUrl ?? request.urlObj, | ||
| result | ||
| ); | ||
| responseBody = renderDirectoryListing(getOriginalUrl(request), result); | ||
| } | ||
|
|
||
| return new Response(responseBody, { | ||
| headers: { | ||
| 'last-modified': result.lastModified.toUTCString(), | ||
| 'content-type': 'text/html', | ||
| 'cache-control': CACHE_HEADERS.success, | ||
| // Directory listings change as files are added. | ||
| 'cache-control': CACHE_HEADERS.mutable, | ||
| }, | ||
| }); | ||
| } | ||
|
|
||
| function setCacheControl( | ||
| result: GetFileResult | HeadFileResult, | ||
| request: Request | ||
| ): void { | ||
| result.httpHeaders['cache-control'] = cacheControlFor( | ||
| getOriginalUrl(request).pathname, | ||
| result.httpStatusCode | ||
| ); | ||
| } | ||
|
|
||
| function handleFile( | ||
| request: Request, | ||
| r2Path: string, | ||
|
|
@@ -99,6 +108,8 @@ async function headFile( | |
| return responses.fileNotFound(request.method); | ||
| } | ||
|
|
||
| setCacheControl(result, request); | ||
|
|
||
| return new Response(undefined, { | ||
| status: result.httpStatusCode, | ||
| headers: result.httpHeaders, | ||
|
|
@@ -121,10 +132,16 @@ async function getFile( | |
| if (err instanceof Error) { | ||
| if (err.message.includes('10020')) { | ||
| // Object name not valid, url probably has some weirdness in it | ||
| return new Response(undefined, { status: 400 }); | ||
| return new Response(undefined, { | ||
| status: 400, | ||
| headers: { 'cache-control': CACHE_HEADERS.failure }, | ||
| }); | ||
| } else if (err.message.includes('10039')) { | ||
| // Range not compatible, probably out of bounds | ||
| return new Response(undefined, { status: 416 }); | ||
| return new Response(undefined, { | ||
| status: 416, | ||
| headers: { 'cache-control': CACHE_HEADERS.failure }, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -135,6 +152,8 @@ async function getFile( | |
| return responses.fileNotFound(request.method); | ||
| } | ||
|
|
||
| setCacheControl(result, request); | ||
|
|
||
| return new Response(result.contents, { | ||
| status: result.httpStatusCode, | ||
| headers: result.httpHeaders, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { describe, test, expect } from 'vitest'; | ||
| import { isImmutablePath, cacheControlFor } from './cache'; | ||
| import { CACHE_HEADERS } from '../constants/cache'; | ||
|
|
||
| describe('isImmutablePath', () => { | ||
| test.each([ | ||
| '/dist/v20.0.0/node-v20.0.0-linux-x64.tar.gz', | ||
| '/download/release/v20.0.0/node-v20.0.0.pkg', | ||
| '/docs/v18.0.0/api/fs.html', | ||
| '/metrics/processed/total-csv.csv', | ||
| ])('returns true for immutable asset `%s`', path => { | ||
| expect(isImmutablePath(path)).toEqual(true); | ||
| }); | ||
|
|
||
| test.each([ | ||
| // Directory listings | ||
| '/dist/', | ||
| '/dist/v20.0.0/', | ||
| // Index/metadata files | ||
| '/dist/index.json', | ||
| '/dist/index.tab', | ||
| '/llms.txt', | ||
| '/node-config-schema.json', | ||
| // `/api/*` always serves the latest version's docs | ||
| '/api', | ||
| '/api/fs.html', | ||
| // `latest` aliases point at moving content | ||
| '/dist/latest/node-v20.0.0-linux-x64.tar.gz', | ||
| '/docs/latest/api/fs.html', | ||
| '/download/release/latest/win-x64/node_pdb.7z', | ||
| '/dist/latest-v20.x/node-v20.0.0.tar.gz', | ||
| // Legacy `latest-v0.X.x` aliases | ||
| '/dist/latest-v0.10.x/node-v0.10.48.tar.gz', | ||
| '/dist/latest-v0.12.x/node-v0.12.18.tar.gz', | ||
| // Codename aliases | ||
| '/dist/latest-argon/node-v4.9.1.tar.gz', | ||
| '/download/release/latest-iron/win-x64/node_pdb.7z', | ||
| '/docs/latest-hydrogen/api/fs.html', | ||
| // `node-latest.tar.gz` is a moving alias too | ||
| '/dist/node-latest.tar.gz', | ||
| ])('returns false for mutable path `%s`', path => { | ||
| expect(isImmutablePath(path)).toEqual(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('cacheControlFor', () => { | ||
| test('immutable asset with 200 gets the immutable policy', () => { | ||
| expect( | ||
| cacheControlFor('/dist/v20.0.0/node-v20.0.0-linux-x64.tar.gz', 200) | ||
| ).toEqual(CACHE_HEADERS.immutable); | ||
| }); | ||
|
|
||
| test('mutable resource with 200 gets the mutable policy', () => { | ||
| expect(cacheControlFor('/dist/index.json', 200)).toEqual( | ||
| CACHE_HEADERS.mutable | ||
| ); | ||
| }); | ||
|
|
||
| test.each([206, 304, 412, 404, 500])( | ||
| 'status %i always gets the failure policy', | ||
| status => { | ||
| expect( | ||
| cacheControlFor('/dist/v20.0.0/node-v20.0.0.tar.gz', status) | ||
| ).toEqual(CACHE_HEADERS.failure); | ||
| } | ||
| ); | ||
| }); |


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In light of https://github.com/nodejs/web-team/blob/main/incidents/2026-03-04.md, debating if we really would want it to be cached this long, especially since what if it happens again and there is a threat actor behind it, wdyt @nodejs/web-infra.
I definitely think it should be cached for longer, but unsure on exactly how long