Skip to content

Instantly share code, notes, and snippets.

View thepassle's full-sized avatar

Pascal Schilp thepassle

View GitHub Profile
@thepassle
thepassle / sw.js
Created October 28, 2019 14:18
lifecycle7
// cache only, or any other caching strategy
self.addEventListener('fetch', function(event) {
event.respondWith(caches.match(event.request));
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:19
lifecycle10
self.addEventListener('activate', event => {
clients.claim();
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:20
skipwaiting6
self.addEventListener('install', event => {
// precache assets etc etc
// ...
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:20
skipwaiting8
self.addEventListener('install', event => {
// precache assets etc etc
self.skipWaiting();
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:21
skipwaiting10
self.addEventListener('activate', event => {
// clean up old caches etc etc
// ...
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:21
skipwaiting11
self.addEventListener('activate', event => {
// clean up old caches etc etc
clients.claim();
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:22
skipwaiting12
self.addEventListener(fetch, event => {
// ready to handle requests!
});
@thepassle
thepassle / main.js
Created October 28, 2019 14:27
updating4
const reg = await navigator.serviceWorker.getRegistration();
reg.addEventListener('updatefound', () => {
// ...
});
@thepassle
thepassle / main.js
Created October 28, 2019 14:27
updating6
const reg = await navigator.serviceWorker.getRegistration();
reg.addEventListener('updatefound', () => {
const newWorker = reg.installing;
newWorker.addEventListener('statechange', () => {
if (newWorker.state === 'installed') {
// Update available! Show an install button
}
});
});
@thepassle
thepassle / main.js
Created October 28, 2019 14:28
updating8
button.addEventListener('click', () => {
newWorker.postMessage({ type: 'SKIP_WAITING' });
});