我有以下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-开发人员工具-选项卡应用程序-缓存存储-清除),我可以再次打开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
};

关于javascript - sw-precache-config,忽略路径不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49953487/

10-14 15:26