Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turn off a service worker in parts of the website #1257

Closed
ghost opened this issue Jan 9, 2018 · 3 comments
Closed

Turn off a service worker in parts of the website #1257

ghost opened this issue Jan 9, 2018 · 3 comments

Comments

@ghost
Copy link

ghost commented Jan 9, 2018

Hi,

Service Workers are great and I love them, but they are very annoying when trying to work in the backend of a website like an admincp.

In Google Chrome there is a feature that you can turn off a service worker for a website - works great!

I just wondered if its possible to write a small JavaScript and add it to all the admincp pages that tells the browser to turn off the service worker automatically.

Because in a real life situation we only need to cache the front-end and not the back-end of a website.

Thanks.

@jakearchibald
Copy link
Contributor

jakearchibald commented Jan 9, 2018

In your service worker's fetch listener:

addEventListener('fetch', event => {
  const url = new URL(event.request.url);

  if (url.origin === location.origin && url.pathname.startsWith('/admin/') {
    // just let the browser do the normal thing:
    return;
  }

  // rest of your service worker logic
});

Adjust the URLs in the code above for wherever your admin pages are.

@inian
Copy link

inian commented Jan 11, 2018

@jakearchibald is there a declarative way to do this? Since there is a non-trivial overhead added by the fetch handler, it would be cool if there was a way to declaratively tell the browser not to go via the service worker for certain pages / resources etc.

@jakearchibald
Copy link
Contributor

There isn't a declarative way. If the overhead is seriously getting in the way, consider navigation preload: https://developers.google.com/web/updates/2017/02/navigation-preload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants