Last active
August 24, 2021 20:36
-
-
Save markvanwijnen/e8098d69bf81c12b5d210ae7e1e1677f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var CACHE_NAME = 'my-web-app-version-v1'; | |
// 1 - Install | |
// Here we wait for an InstallEvent to fire, then tell the cache which URLs it should retrieve and cache | |
self.addEventListener('install', event => { | |
event.waitUntil( | |
caches.open(CACHE_NAME) | |
.then(cache => { | |
return cache.addAll([ | |
'./', | |
'./index.php' | |
]); | |
}) | |
); | |
}); | |
// 2 - Activate | |
// Here we wait for an ActivateEvent to fire, then we remove any old cache. That is to say, any cache stored | |
// that does not have the same key as the one we defined in CACHE_NAME | |
self.addEventListener('activate', event => { | |
event.waitUntil( | |
caches.keys().then(cacheNames => { | |
return Promise.all( | |
cacheNames.map(cacheName => { | |
if (cacheName !== CACHE_NAME) | |
return caches.delete(cacheName); | |
}) | |
); | |
}) | |
); | |
}); | |
// 3 - Fetch | |
// Anytime our website requests a new resource we first check the cache if we have anything available already | |
// and if so, just use that. If not, we retrieve the resource and put it in cache for the next time we may need it. | |
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) | |
}); | |
}); | |
}) | |
) | |
}); | |
// 4 - Message | |
// Here we wait for a MessageEvent to fire, if the message contains skipWaiting we should execute that method. | |
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