创建 React App Service Worker nginx 无缓存配置

Lus*_*Das 5 facebook nginx service-worker create-react-app

我试图在创建 React 应用程序项目中通过 nginx 设置服务工作人员的缓存标头,在配置中,我尝试了

location /service-worker.js {
  add_header Cache-Control "no-cache";
  proxy_cache_bypass $http_pragma;
  proxy_cache_revalidate on;
  expires off;
  access_log off;
}
Run Code Online (Sandbox Code Playgroud)

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

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 方法吗?

Moh*_*hit 3

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

请检查该文件是否存在。如果您使用的是express 和express static 并将文件放置在public/assets 目录中,则它将无法工作。如果您希望该文件有不同的位置。你可以使用alias指令。