9

我可以在使用 sw precache 生成 service worker 时排除一些 url。下面是我的 swprecache.config.json

module.exports = {
navigateFallback: '/index.html',
stripPrefix: 'dist',
root: 'dist/',
staticFileGlobs: [
  'dist/index.html',
  'dist/**.js',
  'dist/**.css',
  'dist/**.ico',
  'dist/assets/images/**.jpg',
  'dist/assets/images/**.png',
  'dist/assets/images/**.gif',
  'dist/assets/js/**/**.js',
  'dist/assets/js/**.js',
  'dist/assets/css/**.css',
  'dist/assets/fonts/**.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}',
  'dist/**.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}',
   '!dist/Subscription/**.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}'
],

runtimeCaching: [{
  urlPattern: /^https:\/\/netdna\.bootstrapcdn\.com\//,
  handler: 'networkFirst'
}]

};

我尝试使用 not 运算符,例如 '!dist/Subscription/**.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}'。但它不起作用。所以我导航到子站点时获取无法匹配任何路由错误。仅清除浏览器数据后,我可以导航到子站点。谁能帮我解决它,请找出我的错误在此处输入图像描述

谢谢

4

2 回答 2

1

这应该工作:

staticFileGlobs: [
  'dist/index.html',
  'dist/*.{js,css,ico}',
  'dist/!(Subscription)/**/*.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}'
]

在这里找到:https ://github.com/GoogleChromeLabs/sw-precache/issues/97

于 2018-03-07T07:25:44.660 回答
0

生成 serviceworker 后,我检查了请求是否在 fetch 事件中包含“订阅”,如下所示,工作正常

 self.addEventListener('fetch', function(event) {
if (event.request.method === 'GET') {
// Should we call event.respondWith() inside this fetch event handler?
// This needs to be determined synchronously, which will give other fetch
// handlers a chance to handle the request if need be.
var shouldRespond;
if (event.request.url.match('^.*(\/Subscription\/).*$')) {
    return false;
}
  // OR

if (event.request.url.indexOf('/Subscription/') !== -1) {
    return false;
}

 .............}})

它工作正常。

于 2018-03-12T12:27:11.567 回答