Setting service worker to exclude certain urls only

前端 未结 5 897
遥遥无期
遥遥无期 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:47

    here's whats working for us in the latest CRA version:

    // serviceWorker.js
    
    window.addEventListener('load', () => {
      if (isAdminRoute()) {
        console.info('unregistering service worker for admin route')
        unregister()
        console.info('reloading')
        window.location.reload()
        return false
      }
    

    we exclude all routes under /admin from the server worker, since we are using a different app for our admin area. you can change it of course for anything you like, here's our function in the bottom of the file:

    function isAdminRoute() {
      return window.location.pathname.startsWith('/admin')
    }
    

提交回复
热议问题