【发布时间】:2022-08-05 02:15:59
【问题描述】:
最好在每次需要时“强制”浏览器获取服务工作者文件,而不是从缓存中加载。 我正在使用 NGINX (1.21) 来提供我的 SPA 的静态文件,我的初始配置 (default.conf) 是:
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
我使用以下location 块来禁用缓存,仅为service-worker.js:
location = /service-worker.js {
add_header Cache-Control \'no-store, no-cache\';
if_modified_since off;
expires off;
etag off;
}
在 Chrome DevTools 中清除应用程序缓存,然后硬重新加载后,浏览器收到 404 not found for service-worker.js,但其他一切正常。
我尝试了不同的“解决方案”,例如将 service worker 块移动到默认块之上、添加别名、添加不同的标头等,但每次它都返回 404。
标签: nginx single-page-application service-worker nginx-location