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:29
updating9
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:29
updating12
navigator.serviceWorker.addEventListener('controllerchange', () => {
window.location.reload();
});
@thepassle
thepassle / main.js
Created October 28, 2019 14:30
installing4
let deferredPrompt;
window.addEventListener('beforeinstallprompt', e => {
e.preventDefault(); // prevent mini info bar popping up
deferredPrompt = e;
// show install button somewhere!
});
@thepassle
thepassle / main.js
Created October 28, 2019 14:31
installing5
button.addEventListener('click', async () => {
deferredPrompt.prompt();
// ...
});
@thepassle
thepassle / main.js
Created October 28, 2019 14:42
installing6
button.addEventListener('click', async () => {
deferredPrompt.prompt();
// ...
});
@thepassle
thepassle / main.js
Created October 28, 2019 14:43
installing7
button.addEventListener('click', async () => {
deferredPrompt.prompt();
const { outcome } = await deferredPrompt.userChoice;
if (outcome === 'accepted') button.setAttribute('hidden', ');
// Send an analytics event for either dismissed or accepted!
});
@thepassle
thepassle / index.html
Created October 28, 2019 14:44
clientsclaim2
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./sw.js');
});
}
// ...
@thepassle
thepassle / index.html
Created October 28, 2019 14:45
clientsclaim4
<script>
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./sw.js');
});
}
setTimeout(() => {
const img = new Image();
@thepassle
thepassle / sw.js
Created October 28, 2019 14:46
clientsclaim5
self.addEventListener('install', event => {
// cache a cat SVG
event.waitUntil(
caches.open('static-v1')
.then(cache => cache.add('cat.svg'))
);
});
@thepassle
thepassle / sw.js
Created October 28, 2019 14:47
clientsclaim7
self.addEventListener('install', event => {
// cache a cat SVG
event.waitUntil(
caches.open('static-v1')
.then(cache => cache.add('cat.svg'))
);
});
self.addEventListener('activate', event => {
clients.claim();