Commit 98d0a966 authored by Julien Enselme's avatar Julien Enselme

Make sure the app shell is always served from the cache

parent 822ae00e
......@@ -2,6 +2,12 @@ const VERSION = '{{ version }}';
const staticCachePrefix = 'static';
const staticCacheName = `${staticCachePrefix}-${VERSION}`;
const dynamicCacheName = 'dynamic';
const appShell = [
'static/icons/aurss.512x512.png',
'static/manifest.json',
'',
'offline/',
].map((partialUrl) => `${location.protocol}//${location.host}/${partialUrl}`);
// In milliseconds.
const networkWaitTime = 2000;
......@@ -12,11 +18,7 @@ self.addEventListener('install', (event) => {
caches.open(staticCacheName)
.then(cache => {
console.log('[SW] Caching app shell');
cache.addAll([
'/static/manifest.json',
'/',
'/offline/',
]);
cache.addAll(appShell);
}),
);
});
......@@ -47,6 +49,12 @@ self.addEventListener('fetch', (event) => {
});
function networkThenCache(event) {
// Always get the app shell from the cache.
if (appShell.includes(event.request.url)) {
console.log('[SW] Requested file from app shell, serving from the cache.');
return getFromCache(event);
}
return Promise.race([
tryToFetchAndSaveInCache(event, dynamicCacheName),
new Promise((resolve, reject) => setTimeout(reject, networkWaitTime))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment