feat(cache): set Cache-Control for sirv static assets and SSR#2969
feat(cache): set Cache-Control for sirv static assets and SSR#2969
Conversation
Share rules in server/cache-static.js. server/main.js wraps the adapter handler because build/client is served before SvelteKit handle. hooks apply the same policy to Kit/SSR responses when no Cache-Control is set yet. Co-authored-by: Cursor <cursoragent@cursor.com>
Greptile SummaryThis PR extracts the
Confidence Score: 4/5Safe to merge — the change is a clean refactoring of existing cache logic into a shared module, with the only new runtime behaviour being that sirv-served static assets now also receive Cache-Control headers. The core patching logic in Only Important Files Changed
Reviews (1): Last reviewed commit: "feat(cache): set Cache-Control for sirv ..." | Re-trigger Greptile |
| if (!directive) return; | ||
| const policy = directive; | ||
|
|
||
| let applied = false; | ||
|
|
||
| /** @param {number} statusCode */ | ||
| function tryApply(statusCode) { | ||
| if (applied || res.headersSent || res.getHeader('Cache-Control')) return; | ||
| if (statusCode !== 200 && statusCode !== 206 && statusCode !== 304) return; | ||
| applied = true; | ||
| res.setHeader('Cache-Control', policy); | ||
| } |
There was a problem hiding this comment.
The
policy variable is an unnecessary alias — directive is already validated non-null by the early return on line 58, so it can be used directly throughout the rest of the function. This is dead indirection.
| if (!directive) return; | |
| const policy = directive; | |
| let applied = false; | |
| /** @param {number} statusCode */ | |
| function tryApply(statusCode) { | |
| if (applied || res.headersSent || res.getHeader('Cache-Control')) return; | |
| if (statusCode !== 200 && statusCode !== 206 && statusCode !== 304) return; | |
| applied = true; | |
| res.setHeader('Cache-Control', policy); | |
| } | |
| if (!directive) return; | |
| let applied = false; | |
| /** @param {number} statusCode */ | |
| function tryApply(statusCode) { | |
| if (applied || res.headersSent || res.getHeader('Cache-Control')) return; | |
| if (statusCode !== 200 && statusCode !== 206 && statusCode !== 304) return; | |
| applied = true; | |
| res.setHeader('Cache-Control', directive); | |
| } |
| res.writeHead = (...args) => { | ||
| if (!res.headersSent) { | ||
| const first = args[0]; | ||
| const code = typeof first === 'number' ? first : (res.statusCode ?? 200); | ||
| tryApply(code); | ||
| } | ||
| return origWriteHead(...args); | ||
| }; |
There was a problem hiding this comment.
The
typeof first === 'number' guard and its fallback are dead code. Node.js ServerResponse.writeHead requires the status code as the first argument — it is always a number. The fallback branch (res.statusCode ?? 200) can never be reached.
| res.writeHead = (...args) => { | |
| if (!res.headersSent) { | |
| const first = args[0]; | |
| const code = typeof first === 'number' ? first : (res.statusCode ?? 200); | |
| tryApply(code); | |
| } | |
| return origWriteHead(...args); | |
| }; | |
| res.writeHead = (...args) => { | |
| if (!res.headersSent) { | |
| tryApply(args[0]); | |
| } | |
| return origWriteHead(...args); | |
| }; |
Share rules in server/cache-static.js. server/main.js wraps the adapter handler because build/client is served before SvelteKit handle. hooks apply the same policy to Kit/SSR responses when no Cache-Control is set yet.
What does this PR do?
(Provide a description of what this PR does.)
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work.)
Related PRs and Issues
(If this PR is related to any other PR or resolves any issue or related to any issue link all related PR and issues here.)
Have you read the Contributing Guidelines on issues?
(Write your answer here.)