開發 Web 頁面時,瀏覽器時而顯示新代碼,時而顯示已刪除的舊代碼(如 onclick 仍為舊函數名)。清除瀏覽器緩存無效。
Nginx 默認開啟 靜態文件緩存(open_file_cache),且未正確配置 Cache-Control 頭,導致:
# 開發環境:禁用緩存
location ~* \.(js|css|html)$ {
add_header Cache-Control "no-store, no-cache, must-revalidate";
expires -1;
open_file_cache off;
}
# 生產環境:長期緩存 + 文件名哈希
location ~* \.(js|css|png|jpg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
app.[contenthash].jscurl -I https://localhost/app.js
# 檢查響應頭 Cache-Control, Expires, ETag