Hijacking img requests
self.addEventListerner('fetch', (event) => {
if (event.request.url.endsWith('.jpg')) {
event.respondWith(
fetch('/img/someLocalImage.jpg')
);
}
}
Handle 404
self.addEventListerner('fetch', (event) => {
event.respondWith(
fetch(event.request).then((response) => {
if (response.status == 404) {
return new Response("<h1>Page not found!</h1>", {
headers: {
'Content-Type': 'text/html',
}
})
}
return response;
}).catch(() => {
return new Response('Unknow fail');
})
)
}
Return assets in cache(s)
self.addEventListerner('fetch', (event) => {
event.respondWith(
fetch(event.request).then((response) => {
if (response.status == 404) {
return new Response("Page not found!")
}
return response;
}).catch(() => {
return new Response('Unknow fail');
})
)
}