fix: add content hash to JS bundles and fix caching strategy

JS files were output as main.js without content hash, combined with
nginx's 30-day immutable cache. Browsers would never fetch updated code.

- Add [contenthash:8] to webpack output filenames in production
- Set index.html to no-cache so browsers always check for new bundles
- Only apply immutable cache to hashed filenames (*.abcdef01.js)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Chever John 2026-06-30 02:25:33 +08:00
parent 091fc24d90
commit 31ea850769
No known key found for this signature in database
GPG Key ID: 2894D52ED1211DF0
2 changed files with 14 additions and 5 deletions

View File

@ -29,14 +29,23 @@ server {
proxy_read_timeout 86400; proxy_read_timeout 86400;
} }
# SPA fallback # SPA fallback index.html must not be cached
location / { location / {
try_files $uri $uri/ /index.html; try_files $uri $uri/ /index.html;
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
} }
# 静态资源缓存 # Hashed static assets long-term cache is safe
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { location ~* \.[0-9a-f]{8}\.(js|css)$ {
expires 30d; expires 30d;
add_header Cache-Control "public, immutable"; add_header Cache-Control "public, immutable";
} }
# Other static assets (images, fonts) moderate cache
location ~* \.(png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires 7d;
add_header Cache-Control "public";
}
} }

View File

@ -14,8 +14,8 @@ module.exports = (env, argv) => {
entry: './src/index.tsx', entry: './src/index.tsx',
output: { output: {
path: path.resolve(__dirname, 'dist'), path: path.resolve(__dirname, 'dist'),
filename: '[name].js', filename: isDev ? '[name].js' : '[name].[contenthash:8].js',
chunkFilename: '[name].chunk.js', chunkFilename: isDev ? '[name].chunk.js' : '[name].[contenthash:8].chunk.js',
publicPath: '/', publicPath: '/',
clean: true, clean: true,
}, },