bring remote assets to local
This commit is contained in:
@ -1,3 +1,4 @@
|
||||
|
||||
self.addEventListener('activate', (e) => {
|
||||
self.clients.claim();
|
||||
});
|
||||
@ -5,44 +6,24 @@ self.addEventListener('activate', (e) => {
|
||||
self.addEventListener('fetch', (e) => {
|
||||
const reqUrl = e.request.url;
|
||||
e.respondWith((async() => {
|
||||
// Open asset cache
|
||||
// Open asset cache and see if this request is in it
|
||||
const cache = await caches.open('main');
|
||||
const match = await caches.match(e.request);
|
||||
|
||||
// Request from network
|
||||
// Request the resource from the network
|
||||
const netRes = fetch(e.request).then((res) => {
|
||||
// Create a new response with updated headers
|
||||
const newHeaders = new Headers(res.headers);
|
||||
newHeaders.set('Permissions-Policy', 'clipboard-write=(self), clipboard-read=(self)');
|
||||
|
||||
const newRes = new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers: newHeaders
|
||||
});
|
||||
|
||||
// Cache non-API responses
|
||||
// If the request was successful and this isn't an API call,
|
||||
// update the cached resource
|
||||
if (res.ok && !reqUrl.match(/\/api\/sftp\/.*$/)) {
|
||||
cache.put(e.request, newRes.clone());
|
||||
cache.put(e.request, res.clone());
|
||||
}
|
||||
|
||||
return newRes;
|
||||
// Return the response
|
||||
return res;
|
||||
}).catch(e => {
|
||||
console.error('Fetch error:', e);
|
||||
if (match) {
|
||||
// Add header to cached response
|
||||
const newHeaders = new Headers(match.headers);
|
||||
newHeaders.set('Permissions-Policy', 'clipboard-write=(self), clipboard-read=(self)');
|
||||
return new Response(match.body, {
|
||||
status: match.status,
|
||||
statusText: match.statusText,
|
||||
headers: newHeaders
|
||||
});
|
||||
}
|
||||
throw e;
|
||||
console.error(e);
|
||||
return match;
|
||||
});
|
||||
|
||||
// Return cached or network response
|
||||
// Return the cached resource if it exists
|
||||
// Otherwise, return the network request
|
||||
return match || netRes;
|
||||
})());
|
||||
});
|
Reference in New Issue
Block a user