为我添加:
ng build --aot --output-hashing=all
只有构建命令是不够的,当你有你的应用程序时
在 CDN 和良好的缓存 nginx 配置之后。
1- 首先是删除 html 文件的缓存(nginx):
location ~ \.(html)$ {
add_header Pragma "no-cache";
add_header Cache-Control "no-store";
add_header strict-transport-security "max-age=31536000";
add_header X-Frame-Options "SAMEORIGIN";
try_files $uri $uri/ /index.html;
}
对于静态文件 (js/css ...) 保持缓存工作(网络性能/可用性):
location ~ \.(css|htc|less|js|js2|js3|js4)$ {
expires 31536000s;
add_header Pragma "public";
add_header Cache-Control "max-age=31536000, public";
try_files $uri $uri/ /index.html;
}
2- 出于测试目的,让 dev/prod 构建完全相同。
最终构建开发命令:
ng build --env=dev --aot=true --output-hashing=all --extract-css=true
3- 我们需要在每次部署客户端浏览器时从服务器加载所有 javascript 文件,而不是从缓存中加载,
即使部署是次要更新。
就像角度有一些错误:
https://github.com/angular/angular-cli/issues/10641
发生在我身上。
我结束了使用 bash 的强大功能,这是我用于杀死缓存的脚本
在使用 package.json 文件的每个开发(产品/开发)上:
"scripts": {
...
"deploy_dev": "ng build --env=dev --aot=true --output-hashing=all --extract-css=true && npm run add_date",
"deploy_prd": "ng build --prod && npm run add_date",
"add_date": "npm run add_date_js && npm run add_date_css && npm run rm_bak_files",
"add_date_js": "for i in dist/*; do if [ -f $i ]; then LC_ALL=C sed -i.bak 's:js\":js?'$(date +%H%M%m%d%y)'\":g' $i; fi done",
"add_date_css": "sed -i.bak 's:css\":css?'$(date +%H%M%m%d%y)'\":g' dist/index.html",
"rm_bak_files": "find dist -name '*.bak' -exec rm -Rf {} \\;"
},
命令说明:
add_date_js:用“js?{date+%H%M%m%d%y}”查找并替换所有文件“js”
add_date_css:在 dist/index.html "css" 中查找并替换为 "css?{date+%H%M%m%d%y}"
rm_bak_files:删除所有.bak文件(网络性能)
这些 sed 命令适用于 GNU/BSD/Mac。
链接:
Angular - Prod Build not generating unique hashes
sed in-place flag that works both on Mac (BSD) and Linux
RE error: illegal byte sequence on Mac OS X
Inline if shell script
How to loop over files in directory and change path and add suffix to filename
Is it possible to build separate CSS file with angular-cli?