10

Can I exclude some urls while using sw precache for generating service worker.Below is my 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'
}]

};

I tried to use not operator like '!dist/Subscription/**.{js,html,css,png,jpg,gif,svg,eot,ttf,woff,ico}'.But its not working.So that I am getting cannot match any route error while navigating to subsite.After clearing browser data only, I can navigate to subsite. Can anyone pls help me to fix it,pls find my error enter image description here

Thanks

kamalav
  • 1,190
  • 4
  • 14
  • 31
  • Ok, but now it looks like you have a angular routing problem instead of a sw-precache problem. What routes do you have defined? – Stef Chäser Mar 07 '18 at 20:37
  • if routing problem means.It should not navigate to subsite for the first time also right? – kamalav Mar 08 '18 at 05:41

2 Answers2

1

this should work:

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

found here: https://github.com/GoogleChromeLabs/sw-precache/issues/97

Stef Chäser
  • 1,911
  • 18
  • 26
0

after generating serviceworker,I checked whether the request contains "Subscription" in fetch event like below which is working fine

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

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

Its working fine.

kamalav
  • 1,190
  • 4
  • 14
  • 31