const currentVersion = '28402'; const addResourcesToCache = async (resources) => { const cache = await caches.open(currentVersion); await cache.addAll(resources); }; const putInCache = async (request, response) => { const cache = await caches.open(currentVersion); await cache.put(request, response); }; const cacheFirst = async ({ request, preloadResponsePromise }) => { // First try to get the resource from the cache const responseFromCache = await caches.match(request); if (responseFromCache) { return responseFromCache; } // Next try to use the preloaded response, if it's there const preloadResponse = await preloadResponsePromise; if (preloadResponse) { console.info('using preload response', preloadResponse); putInCache(request, preloadResponse.clone()); return preloadResponse; } // Next try to get the resource from the network try { const responseFromNetwork = await fetch(request); // response may be used only once // we need to save clone to put one copy in cache // and serve second one putInCache(request, responseFromNetwork.clone()); return responseFromNetwork; } catch (error) { return new Response('Network error happened', { status: 408, headers: { 'Content-Type': 'text/plain' }, }); } }; const deleteCache = async key => { await caches.delete(key) } const deleteOldCaches = async () => { const cacheKeepList = [currentVersion]; const keyList = await caches.keys() const cachesToDelete = keyList.filter(key => !cacheKeepList.includes(key)) await Promise.all(cachesToDelete.map(deleteCache)); } self.addEventListener('activate', (event) => { event.waitUntil(deleteOldCaches()); }); const enableNavigationPreload = async () => { if (self.registration.navigationPreload) { // Enable navigation preloads! await self.registration.navigationPreload.enable(); } }; self.addEventListener('activate', (event) => { event.waitUntil(enableNavigationPreload()); }); self.addEventListener('install', (event) => { event.waitUntil( addResourcesToCache([ './heonian-resources/wordlist.json', './heonian-ime/ime.js', './font.otf', './index.html', './main.css', './main.js', './manifest.json' ]) ); }); self.addEventListener('fetch', (event) => { event.respondWith( cacheFirst({ request: event.request, preloadResponsePromise: event.preloadResponse }) ); });