Skip to content

Instantly share code, notes, and snippets.

@AkimaLunar
Created March 9, 2018 18:20
Show Gist options
  • Save AkimaLunar/07afd816bec0b0200f91c8744f45278b to your computer and use it in GitHub Desktop.
Save AkimaLunar/07afd816bec0b0200f91c8744f45278b to your computer and use it in GitHub Desktop.
Service worker registration
// main.js
if (!('serviceWorker' in navigator)) {
console.log('sw not supported');
return;
}
navigator.serviceWorker.register('/path/to/service-worker.js') // returns a Promise
.then(function(registration) {
console.log('SW registered! Scope is: ', registration.scope);
});
//------------------------
// service-worker.js
// Events: install, activate, message, fetch, sync, push...
self.addEventLister('activate', function(event) {
// Clean up stale sw's
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
)
});
//------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment