v0.8.2 部署缓存确定性:静态资源补齐 Cache-Control(哈希产物 immutable,入口/SW no-cache),SW 导航强制校验并升级缓存版本

This commit is contained in:
root
2026-11-01 22:32:36 +08:00
parent 2ee5f5760f
commit 5909eabba8
9 changed files with 23 additions and 13 deletions
+4 -2
View File
@@ -331,16 +331,18 @@ async function handle(req, res) {
if (rel === '/' || rel === '') rel = '/index.html'
const file = path.normalize(path.join(DIST_DIR, rel))
if (!file.startsWith(DIST_DIR)) { json(res, 403, { error: 'forbidden' }); return }
// 带内容哈希的构建产物可永久缓存;入口与 SW 每次校验,保证部署后立刻拿到新包
const cacheControl = rel.startsWith('/assets/') ? 'public, max-age=31536000, immutable' : 'no-cache'
fs.readFile(file, (err, data) => {
if (err) {
fs.readFile(path.join(DIST_DIR, 'index.html'), (e2, d2) => {
if (e2) { json(res, 404, { error: 'not found' }); return }
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' })
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8', 'Cache-Control': 'no-cache' })
res.end(d2)
})
return
}
res.writeHead(200, { 'Content-Type': MIME[path.extname(file).toLowerCase()] || 'application/octet-stream' })
res.writeHead(200, { 'Content-Type': MIME[path.extname(file).toLowerCase()] || 'application/octet-stream', 'Cache-Control': cacheControl })
res.end(data)
})
return