ServiceWorker should cache non-local attachments to cache.

This commit is contained in:
Timothy Farrell 2018-06-22 08:01:12 -05:00
parent a8bab0c4fa
commit 91d45f41dc
3 changed files with 49 additions and 23 deletions

View File

@ -24,9 +24,9 @@ if (location.protocol !== 'https:' && (!crypto.subtle || !crypto.subtle.digest)
location.assign(url); location.assign(url);
} else if ('serviceWorker' in navigator && window.top === window) { } else if ('serviceWorker' in navigator && window.top === window) {
navigator.serviceWorker navigator.serviceWorker
.register('/assets/sw.bundle.js', { scope: '/' }) .register('/sw.bundle.js', { scope: '/' })
.then(go) .then(go)
.catch(go); .catch(err => console.log(err));
} else { } else {
go(); go();
} }

View File

@ -1,39 +1,63 @@
import { FileType } from './data/file.js'; import { FileType } from './data/file.js';
import { log } from './services/console.js'; import { log } from './services/console.js';
const http404 = url => () => { const VERSION = 'v1';
log(`ServiceWorker could not find ${url}`);
function http404() {
return new Response(null, { return new Response(null, {
status: 404, status: 404,
statusText: 'NOT FOUND', statusText: 'NOT FOUND',
headers: {} headers: {}
}); });
}; }
function blobAsResponse(blob) {
return new Response(blob, {
status: 200,
statusText: 'OK',
headers: {
'Content-Type': blob.type
}
});
}
self.addEventListener('fetch', event => { self.addEventListener('fetch', event => {
const requestUrl = new URL(event.request.url); const requestUrl = new URL(event.request.url);
if (requestUrl.pathname && requestUrl.pathname.startsWith('/file/')) { if (requestUrl.pathname && requestUrl.pathname.startsWith('/file/')) {
event.respondWith( event.respondWith(
FileType.getFromURL(requestUrl.pathname) caches
.then(data => { .match(event.request)
if (data) { .then(resp => {
log( if (resp) {
`ServiceWorker serving ${requestUrl.pathname} ${data.type} ${(data.size / 1024).toFixed( log(`ServiceWorker serving ${requestUrl.pathname} from cache.`);
2 return resp;
)}kb`
);
return new Response(data, {
status: 200,
statusText: 'OK',
headers: {
'Content-Type': data.type
}
});
} }
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))
); );
} }
}); });

View File

@ -13,7 +13,9 @@ async function readStorageMap(blob) {
} }
export function PouchDBAttachmentProxy({ save, getFn, remove }) { export function PouchDBAttachmentProxy({ save, getFn, remove }) {
const override = {}; const override = {
$attachmentsProxied: true
};
if (getFn) { if (getFn) {
override.getAttachment = async function getAttachment(...args) { override.getAttachment = async function getAttachment(...args) {