48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
self.addEventListener('activate', (e) => {
|
|
self.clients.claim();
|
|
});
|
|
|
|
self.addEventListener('fetch', (e) => {
|
|
const reqUrl = e.request.url;
|
|
e.respondWith((async() => {
|
|
// Open asset cache
|
|
const cache = await caches.open('main');
|
|
const match = await caches.match(e.request);
|
|
|
|
// Request from 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 (res.ok && !reqUrl.match(/\/api\/sftp\/.*$/)) {
|
|
cache.put(e.request, newRes.clone());
|
|
}
|
|
|
|
return newRes;
|
|
}).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;
|
|
});
|
|
|
|
// Return cached or network response
|
|
return match || netRes;
|
|
})());
|
|
}); |