From 31ea8507694758179477bfa26dce820d8e0d36c6 Mon Sep 17 00:00:00 2001 From: Chever John Date: Tue, 30 Jun 2026 02:25:33 +0800 Subject: [PATCH] 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 --- nginx.conf | 15 ++++++++++++--- webpack.config.js | 4 ++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/nginx.conf b/nginx.conf index 4a5e4ad..095793d 100644 --- a/nginx.conf +++ b/nginx.conf @@ -29,14 +29,23 @@ server { proxy_read_timeout 86400; } - # SPA fallback + # SPA fallback — index.html must not be cached location / { 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"; } - # 静态资源缓存 - location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { + # Hashed static assets — long-term cache is safe + location ~* \.[0-9a-f]{8}\.(js|css)$ { expires 30d; 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"; + } } diff --git a/webpack.config.js b/webpack.config.js index bacac91..8630754 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -14,8 +14,8 @@ module.exports = (env, argv) => { entry: './src/index.tsx', output: { path: path.resolve(__dirname, 'dist'), - filename: '[name].js', - chunkFilename: '[name].chunk.js', + filename: isDev ? '[name].js' : '[name].[contenthash:8].js', + chunkFilename: isDev ? '[name].chunk.js' : '[name].[contenthash:8].chunk.js', publicPath: '/', clean: true, },