diff --git a/packages/gallery/src/app.js b/packages/gallery/src/app.js index 70f68d8..db5f0ad 100644 --- a/packages/gallery/src/app.js +++ b/packages/gallery/src/app.js @@ -24,9 +24,9 @@ if (location.protocol !== 'https:' && (!crypto.subtle || !crypto.subtle.digest) location.assign(url); } else if ('serviceWorker' in navigator && window.top === window) { navigator.serviceWorker - .register('/assets/sw.bundle.js', { scope: '/' }) + .register('/sw.bundle.js', { scope: '/' }) .then(go) - .catch(go); + .catch(err => console.log(err)); } else { go(); } diff --git a/packages/gallery/src/sw.js b/packages/gallery/src/sw.js index a79b7ad..e721f59 100644 --- a/packages/gallery/src/sw.js +++ b/packages/gallery/src/sw.js @@ -1,39 +1,63 @@ import { FileType } from './data/file.js'; import { log } from './services/console.js'; -const http404 = url => () => { - log(`ServiceWorker could not find ${url}`); +const VERSION = 'v1'; + +function http404() { return new Response(null, { status: 404, statusText: 'NOT FOUND', headers: {} }); -}; +} + +function blobAsResponse(blob) { + return new Response(blob, { + status: 200, + statusText: 'OK', + headers: { + 'Content-Type': blob.type + } + }); +} self.addEventListener('fetch', event => { const requestUrl = new URL(event.request.url); if (requestUrl.pathname && requestUrl.pathname.startsWith('/file/')) { event.respondWith( - FileType.getFromURL(requestUrl.pathname) - .then(data => { - if (data) { - log( - `ServiceWorker serving ${requestUrl.pathname} ${data.type} ${(data.size / 1024).toFixed( - 2 - )}kb` - ); - return new Response(data, { - status: 200, - statusText: 'OK', - headers: { - 'Content-Type': data.type - } - }); + caches + .match(event.request) + .then(resp => { + if (resp) { + log(`ServiceWorker serving ${requestUrl.pathname} from cache.`); + return resp; } - return http404(requestUrl.pathname)(); + return FileType.getFromURL(requestUrl.pathname).then(blob => { + if (blob) { + log( + `ServiceWorker serving ${requestUrl.pathname} ${blob.type} ${(blob.size / 1024).toFixed( + 2 + )}kb` + ); + return blobAsResponse(blob); + } + throw new NotFoundError(requestUrl.pathname); + }); + }) + .then(resp => { + if (!FileType.db.$attachmentsProxied) { + return resp; + } + return caches + .open(VERSION) + .then(cache => cache.put(event.request, resp.clone())) + .then(_ => resp); + }) + .catch(err => { + log(`ServiceWorker could not access ${requestUrl.pathname}`, err); + return http404(); }) - .catch(http404(requestUrl.pathname)) ); } }); diff --git a/packages/gallery/src/utils/attachmentProxy.js b/packages/gallery/src/utils/attachmentProxy.js index ff29888..0179a37 100644 --- a/packages/gallery/src/utils/attachmentProxy.js +++ b/packages/gallery/src/utils/attachmentProxy.js @@ -13,7 +13,9 @@ async function readStorageMap(blob) { } export function PouchDBAttachmentProxy({ save, getFn, remove }) { - const override = {}; + const override = { + $attachmentsProxied: true + }; if (getFn) { override.getAttachment = async function getAttachment(...args) {