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);
|
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();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 => {
|
||||||
|
if (resp) {
|
||||||
|
log(`ServiceWorker serving ${requestUrl.pathname} from cache.`);
|
||||||
|
return resp;
|
||||||
|
}
|
||||||
|
return FileType.getFromURL(requestUrl.pathname).then(blob => {
|
||||||
|
if (blob) {
|
||||||
log(
|
log(
|
||||||
`ServiceWorker serving ${requestUrl.pathname} ${data.type} ${(data.size / 1024).toFixed(
|
`ServiceWorker serving ${requestUrl.pathname} ${blob.type} ${(blob.size / 1024).toFixed(
|
||||||
2
|
2
|
||||||
)}kb`
|
)}kb`
|
||||||
);
|
);
|
||||||
return new Response(data, {
|
return blobAsResponse(blob);
|
||||||
status: 200,
|
|
||||||
statusText: 'OK',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': data.type
|
|
||||||
}
|
}
|
||||||
|
throw new NotFoundError(requestUrl.pathname);
|
||||||
});
|
});
|
||||||
}
|
|
||||||
return http404(requestUrl.pathname)();
|
|
||||||
})
|
})
|
||||||
.catch(http404(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();
|
||||||
|
})
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -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) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user