Javascript sw precache配置,忽略路径不工作?

Javascript sw precache配置,忽略路径不工作?,javascript,service-worker,Javascript,Service Worker,我有以下sw-precache-config.js module.exports = { staticFileGlobs: [ 'index.html', 'manifest.json', 'bower_components/webcomponentsjs/*', ], navigateFallback: 'index.html', runtimeCaching: [{ urlPattern: /.+\/api\/.+/, handler:

我有以下sw-precache-config.js

module.exports = {
  staticFileGlobs: [
    'index.html',
    'manifest.json',
    'bower_components/webcomponentsjs/*',
  ],
  navigateFallback: 'index.html',
  runtimeCaching: [{
    urlPattern: /.+\/api\/.+/,
    handler: 'networkOnly',
  }],
};
我有一个带有service worker(使用上述配置生成)的SPA应用程序来缓存资源

当我在没有缓存资源的情况下打开myapp/api/route(第一次访问网站)时,它工作正常

如果我打开myapp的主页,浏览器会缓存资源,对myapp/api/route的后续请求会导致问题(生成了myapp主页的布局,但没有数据)

当我清除浏览器的缓存(Chrome-Developer Tools-tab Application-cache storage-clear)时,我可以再次打开myapp/api/route


因此,我的问题是:如何强制浏览器忽略所有“myapp/api/*”路由的service-worker.js?

您只需忽略包含该模式的请求即可

// service-worker.js   
self.onfetch = evt => {
  let path = new URL(evt.request.url).pathname;
  if(path.startsWith('/myapp/api')) {
    // these requests will be networked
    return;
  }
  // Handle other requests here
};

service worker是自动生成的,所以我如何强制生成它?@vitaly这应该是另一个话题。您可以搜索更新服务人员的方法。我想已经有答案了。