ServiceWorker should cache non-local attachments to cache.
This commit is contained in:
parent
a105cfb550
commit
69b8289213
@ -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();
|
||||
}
|
||||
|
||||
@ -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))
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user