Search code examples
angularangular5service-workersw-precache

Exclude subsite urls while using sw precache in angular5


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


Solution

  • 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.