我正在为 Gatsby 设置 Nginx 服务器(版本 1.17.1),以遵循https://www.gatsbyjs.org/docs/caching/上的建议。
下面的片段是我server {}尝试实现推荐的缓存配置的部分;
location ~* \.(?:html)$ {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
location /static {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location ~* \.(?:css|js)$ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location /sw\.js {
add_header Cache-Control "public, max-age=0, must-revalidate";
}
同样尝试使用if 语句代替location {}用于定义服务工作者文件的缓存配置的块sw.js,如下所示;
if ($request_uri ~ ^sw\.(?:js)$) {
set $no_cache 1;
}
不幸的是,所有文件都按预期成功缓存,除了sw.js.
我做错了什么,如何解决它以便有效地将缓存控制标头设置为sw.jsto public, max-age=0, must-revalidate?