Skip to content

Instantly share code, notes, and snippets.

@markvanwijnen
Created August 24, 2021 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markvanwijnen/b473e49605e94f1e517ce4342afab238 to your computer and use it in GitHub Desktop.
Save markvanwijnen/b473e49605e94f1e517ce4342afab238 to your computer and use it in GitHub Desktop.
var CACHE_NAME = 'my-web-app-version-v3';
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
return cache.addAll([
'./',
'./index.php',
'./ServiceWorkerUpdateListener.min.js'
]);
})
);
});
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cacheName => {
if (cacheName !== CACHE_NAME)
return caches.delete(cacheName);
})
);
})
);
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => {
if (response) return response;
return fetch(event.request)
.then(response => {
if (!response || response.status !== 200 || response.type !== 'basic') return response;
var responseToCache = response.clone();
caches.open(CACHE_NAME).then(cache => {
cache.put(event.request, responseToCache)
});
});
})
)
});
self.addEventListener('message', event => {
if (event.data === 'skipWaiting') return skipWaiting();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment