Skip to content

fix: 修复 SPA 入口被缓存导致部署后仍加载旧前端 - #6

Merged
mzrodyu merged 1 commit into
mainfrom
feat/model-pull-button
Sep 10, 2026
Merged

mzrodyu merged 1 commit into
mainfrom
feat/model-pull-button

Conversation

@mzrodyu

@mzrodyu mzrodyu commented Sep 10, 2026

Copy link
Copy Markdown
Owner

问题

index.html 会被所有未匹配路由返回,且文件名不带内容哈希,但静态处理完全没有设置 Cache-Control

GET /                          → 200, 仅 Last-Modified
GET /assets/index-CkIGdnVN.js  → 200, 仅 Last-Modified

浏览器因此走启发式缓存,Cloudflare 等中间层也会按扩展名缓存 .html。结果是重新部署后客户端仍加载旧 bundle——这正是账号页新 UI(例如删除 API Key 按钮)始终不出现的直接原因。

修复

  • index.html 及其他无哈希文件:Cache-Control: no-cache, must-revalidate
  • Vite 内容哈希产物:Cache-Control: public, max-age=31536000, immutable(内容变则文件名变,可安全长缓存)

验证

GET /                          → Cache-Control: no-cache, must-revalidate
GET /assets/index-CkIGdnVN.js  → Cache-Control: public, max-age=31536000, immutable
GET /favicon.svg               → Cache-Control: no-cache, must-revalidate

新增 TestStaticServingSetsCacheHeadersForSPAEntryPointTestIsImmutableBuildAssetgo test ./... 全绿。

index.html is returned for every unmatched route and carries no content hash,
but the static handler set no Cache-Control at all. Browsers therefore applied
heuristic freshness and intermediaries such as Cloudflare cached it by
extension, pinning clients to whatever bundle existed at cache time. Redeploys
appeared to do nothing, which is what made the new account-page UI (for example
the API key delete button) keep showing an older build.

- index.html and other unhashed files: no-cache, must-revalidate
- Vite's content-hashed assets: public, max-age=31536000, immutable

Co-Authored-By: Claude Code <noreply@anthropic.com>
@mzrodyu
mzrodyu merged commit 0c8ac82 into main Sep 10, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant