在angular5中使用sw预缓存时排除子网站网址

时间:2018-03-02 09:53:33

标签: angular angular5 service-worker sw-precache

我可以在使用sw预缓存生成服务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'
}]

};

我尝试使用非运营商,例如'!dist / Subscription / **。{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}'。但它不起作用。因此我在获取导航到子网站时无法匹配任何路由错误。仅清除浏览器数据后,我可以导航到子网站。任何人都可以帮我修复它,请找我的错误enter image description here

由于

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

在生成serviceworker之后,我检查了请求是否包含" Subscription"在如下所示的获取事件中工作正常

 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;
}

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

工作正常。