sw: 与nginx配置
配置的时候注意
🕐
关键配置
# 匹配所有常见的 Service Worker 文件名
location ~ ^/(sw|service-worker|sw-registration|firebase-messaging-sw|custom-sw)\.js$ {
try_files $uri =404;
}
# 上面简单的情况(一般不会改这个文件,用这段即可)
location /sw.js {
try_files $uri =404;
}
# SPA 应用的主路由处理
location / {
try_files $uri $uri/ /index.html;
}工作中的场景
实际中的报错,拉不到
sw.js导致最终更新卡在这里。
Failed to update a ServiceWorker for scope ('https://dev.demo.com/') with script ('https://dev.demo.com/sw.js'): An unknown error occurred when fetching the script

🔗 参考链接
- https://chatgpt.com/c/66e78b3e-d818-8013-b8e9-33e6e6b03334
- https://www.hozen.site/archives/16/
- https://github.com/afeiship/react-sw-updater
- https://github.com/lavas-project/lavas/issues/212
- https://lavas-project.github.io/pwa-book/chapter04/3-service-worker-dive.html
- https://nanmu.me/zh-cn/posts/2020/why-my-content-served-by-pwa-service-worker-not-updating/