创建react app service worker nginx无缓存配置

Create react app service worker nginx no cache configuration

我正在尝试通过 create react app project 中的 nginx 为 service worker 设置缓存 header,在配置中,我尝试了

location /service-worker.js {
  add_header Cache-Control "no-cache";
  proxy_cache_bypass $http_pragma;
  proxy_cache_revalidate on;
  expires off;
  access_log off;
}

然而,当我加载我的页面时,软件注册失败并显示消息。

A bad HTTP response code (404) was received when fetching the script. registerServiceWorker.js:71 Error during service worker registration: TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.

有人可以建议使用 create-react-app 的 nginx 方法吗?

根据您的配置 service-worker.js 必须位于 / 根目录中,根目录由 root nginx 指令定义。

请检查文件是否存在。如果您使用 express 和 express static 并将文件放在 public/assets 目录中,它将不起作用。如果对于这个文件你想有不同的位置。你可以使用 alias 指令。