generated from nhcarrigan/template
feat: Multiple Features, Accessibility, Security, and UX Improvements #59
@@ -92,18 +92,25 @@ self.addEventListener('fetch', (event) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Images - Cache first, network fallback
|
// Images - Cache first, network fallback (only for same-origin)
|
||||||
if (request.destination === 'image' || url.pathname.match(/\.(jpg|jpeg|png|gif|webp|svg)$/)) {
|
if (request.destination === 'image' || url.pathname.match(/\.(jpg|jpeg|png|gif|webp|svg)$/)) {
|
||||||
|
// Don't cache external images
|
||||||
|
if (url.origin !== self.location.origin) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(request).then((cachedResponse) => {
|
caches.match(request).then((cachedResponse) => {
|
||||||
if (cachedResponse) {
|
if (cachedResponse) {
|
||||||
return cachedResponse;
|
return cachedResponse;
|
||||||
}
|
}
|
||||||
return fetch(request).then((response) => {
|
return fetch(request).then((response) => {
|
||||||
const responseClone = response.clone();
|
if (response.ok) {
|
||||||
caches.open(IMAGE_CACHE).then((cache) => {
|
const responseClone = response.clone();
|
||||||
cache.put(request, responseClone);
|
caches.open(IMAGE_CACHE).then((cache) => {
|
||||||
});
|
cache.put(request, responseClone);
|
||||||
|
});
|
||||||
|
}
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
@@ -111,18 +118,25 @@ self.addEventListener('fetch', (event) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Static assets - Cache first, network fallback
|
// Static assets - Cache first, network fallback (only for same-origin)
|
||||||
if (url.pathname.match(/\.(js|css|woff|woff2|ttf|eot)$/)) {
|
if (url.pathname.match(/\.(js|css|woff|woff2|ttf|eot)$/)) {
|
||||||
|
// Don't cache external scripts/styles (CDN resources, analytics, etc.)
|
||||||
|
if (url.origin !== self.location.origin) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
caches.match(request).then((cachedResponse) => {
|
caches.match(request).then((cachedResponse) => {
|
||||||
if (cachedResponse) {
|
if (cachedResponse) {
|
||||||
return cachedResponse;
|
return cachedResponse;
|
||||||
}
|
}
|
||||||
return fetch(request).then((response) => {
|
return fetch(request).then((response) => {
|
||||||
const responseClone = response.clone();
|
if (response.ok) {
|
||||||
caches.open(STATIC_CACHE).then((cache) => {
|
const responseClone = response.clone();
|
||||||
cache.put(request, responseClone);
|
caches.open(STATIC_CACHE).then((cache) => {
|
||||||
});
|
cache.put(request, responseClone);
|
||||||
|
});
|
||||||
|
}
|
||||||
return response;
|
return response;
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user