routing.headers cannot set Cache-Control on static assets
維護者通常 1 天內回覆
還沒有人認領這個 Issue。
評估
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 新手友好度
- 56/100
- Issue 類型
- 缺陷
- 描述清晰度
- 描述清楚
- 活躍度
- 活躍
- 技術堆疊
- typescript
- 領域
- backend
研究方向
Start by reading headerMiddleware in dist/runtime/routing.mjs, then inspect stampClientAssetCachePolicy and the routing runtime tests. Reproduce the CSS and JS behavior with vp build and vp preview, and check how the fix affects 404 responses. Done means the acceptance checklist passes in preview and Cloudflare deploy, with tests and docs covering the 404 policy.
由索引模型根據 Issue 內容生成。
描述
routing.headers cannot set Cache-Control on static assets
What happens
With Void 1.1.12 (the runtime code is the same in 1.2.1), this rule has no effect on static assets:
routing: {
headers: {
'/assets/*': ['Cache-Control: public, max-age=31536000, immutable'],
},
},
A CSS file, a font or an image under /assets/ still answers cache-control: public, max-age=0, must-revalidate. Other headers in the same rule, such as X-Robots-Tag, do arrive.
The rule also makes JavaScript worse. stampClientAssetCachePolicy removes immutableFiles for every chunk an authored Cache-Control pattern matches. So JS chunks lose the built-in immutable policy, and the authored policy never applies either.
The docs say the opposite should happen. guide/edge/headers.md says "User-defined Cache-Control overrides the built-in default", and guide/edge/static-assets.md suggests routing.headers for CSS and other assets.
Cause
headerMiddleware in dist/runtime/routing.mjs runs after next() and assigns a new response:
const headers = new Headers(c.res.headers);
for (const [name, value] of matched) headers.set(name, value);
c.res = new Response(c.res.body, { headers, status: c.res.status, statusText: c.res.statusText });
Hono's set res copies every header of the previous #res onto the new response, except content-type and set-cookie. The previous response is the asset binding's response, which carries cache-control: public, max-age=0, must-revalidate. That value overwrites the authored one. Headers the old response lacks, such as X-Robots-Tag, survive, which hides the bug.
Reproduce
- In a static Void app with
output: 'static', add the rule above. - Run
vp build, thenvp preview. - Run
curl -s -D - -o /dev/null http://127.0.0.1:<port>/assets/<any>.css. The response showscache-control: public, max-age=0, must-revalidate. - Request a JS chunk the same way. It now also shows
max-age=0, must-revalidate, where it showedimmutablebefore the rule was added.
Fix we apply locally
Clear the context's response before you assign the new one, so Hono has nothing to merge:
const response = new Response(c.res.body, { headers, status: c.res.status, statusText: c.res.statusText });
c.res = void 0;
c.res = response;
Aureum carries this as patches/void.patch.
Also worth deciding
Header rules apply to every status. With the fix in place, /assets/missing.css answers 404 with immutable, so a browser can cache the 404 for a year. A deploy that briefly serves new HTML from one version and assets from another could leave a visitor with a stylesheet that stays broken. Consider applying authored Cache-Control to successful responses only, or document that it applies to every status.
Acceptance checklist
- A
routing.headersrule that setsCache-Controlfor/assets/*reaches CSS, font and image responses invp previewand in a Cloudflare deploy. - JS chunks under the same pattern answer with the authored
Cache-Control. - Pages and other paths the rule does not match keep their default
Cache-Control. - A test in the routing runtime covers an authored header that the asset response already carries.
- The docs state whether authored
Cache-Controlapplies to 404 responses.
- 主要語言
- TypeScript
- 星號
- 580
- 分支
- 56
- 平均合併
- 12 小時 25 分鐘
- 30 天內合併 PR
- 134
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
voidzero-dev/void 的其他 Issue
-
難度 3/5 1-2 天 新手友好度 66/100
voidzero-dev/void#599 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 82/100
voidzero-dev/void#596 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 72/100
voidzero-dev/void#575 ·
維護者通常 1 天內回覆
-
難度 3/5 1-2 天 新手友好度 55/100
voidzero-dev/void#573 ·
維護者通常 1 天內回覆
-
難度 4/5 3-5 天 新手友好度 53/100
voidzero-dev/void#567 · 1 個 reaction ·
維護者通常 1 天內回覆
查看 voidzero-dev/void 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
VictoriaMetrics/victorialogs-datasource#747 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 79/100
維護者通常 1 天內回覆
-
bug from-studio
難度 2/5 1-3 小時 新手友好度 62/100
esengine/DeepSeek-Reasonix#14608 ·
維護者通常 1 天內回覆
-
triage
難度 2/5 1-3 小時 新手友好度 75/100
getarcaneapp/arcane#4590 ·
維護者通常 1 天內回覆
-
難度 2/5 1-3 小時 新手友好度 77/100
Clouditera/VulnHunter#57 ·