Setting service worker to exclude certain urls only

前端 未结 5 895
遥遥无期
遥遥无期 2020-12-05 07:26

I built an app using create react which by default includes a service worker. I want the app to be run anytime someone enters the given url except when they go to /blog/, wh

5条回答
  •  無奈伤痛
    2020-12-05 07:53

    So, considering, you have not posted any code relevant to the service worker, you might consider adding a simple if conditional inside the code block for fetch

    This code block should already be there inside your service worker.Just add the conditionals

    self.addEventListener( 'fetch', function ( event ) {
    
        if ( event.request.url.match( '^.*(\/blog\/).*$' ) ) {
            return false;
        }
         // OR
    
        if ( event.request.url.indexOf( '/blog/' ) !== -1 ) {
            return false;
        }
        //    **** rest of your service worker code ****
    

    note you can either use the regex or the prototype method indexOf. per your whim.

    the above would direct your service worker, to just do nothing when the url matches /blog/

提交回复
热议问题