|
self.addEventListener("fetch", function(event) { |
|
|
|
console.log("Fetch request for:", event.request.url); |
|
|
|
}); |
|
|
|
/* |
|
| -------------------------------------------------------------------------- |
|
| Intercept request and respond with something else |
|
| -------------------------------------------------------------------------- |
|
| Here if bootrap.min.css is requested we respond with a custom CSS |
|
| |
|
|
|
self.addEventListener("fetch", function(event) { |
|
if (event.request.url.includes("bootstrap.min.css")) { |
|
event.respondWith( |
|
new Response( ".hotel-slogan {background: green!important;} nav {display:none}", |
|
{ headers: { "Content-Type": "text/css" }} |
|
) |
|
); |
|
} |
|
}); |
|
*/ |
|
|
|
|
|
/* |
|
| -------------------------------------------------------------------------- |
|
| We can listen an image request and replace it |
|
| -------------------------------------------------------------------------- |
|
| |
|
*/ |
|
self.addEventListener("fetch", function(event) { |
|
|
|
if (event.request.url.includes("/img/logo.png")) { |
|
event.respondWith( |
|
fetch("/img/logo-flipped.png") |
|
); |
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
/* |
|
| -------------------------------------------------------------------------- |
|
| Fetch return a promise : so we can catch if something goes wrong |
|
| -------------------------------------------------------------------------- |
|
| |
|
| In this example : if the request can't be made we awnser with a text response |
|
| |
|
*/ |
|
|
|
var responseContent = "<html>" + |
|
"<body>" + |
|
"<style>" + |
|
"body {text-align: center; background-color: #333; color: #eee;}"+ |
|
"</style>" + |
|
"<h1>Gotham Imperial Hotel</h1>" + |
|
"<p>There seems to be a problem with your connection.</p>" + |
|
"<p>Come visit us at 1 Imperial Plaza, Gotham City for free WiFi.</p>"+ |
|
"</body>" + |
|
"</html>"; |
|
|
|
|
|
self.addEventListener("fetch", function(event) { |
|
|
|
event.respondWith( |
|
fetch(event.request).catch(function() { |
|
return new Response(responseContent, { headers: { "Content-Type": "text/html; charset=utf-8" }} |
|
); |
|
}) |
|
); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
| -------------------------------------------------------------------------- |
|
| Delete all old caches |
|
| -------------------------------------------------------------------------- |
|
| |
|
*/ |
|
self.addEventListener("activate", function(event) { // Listen for the activate event. |
|
event.waitUntil( // Wait until the following is done and only declare the service worker activated |
|
caches.keys().then(function(cacheNames) { |
|
return Promise.all( // if all of the following complete successfully: |
|
cacheNames.map(function(cacheName) { // For each of the cache names: |
|
console.log('cache ', cacheName ); |
|
if ( CACHE_NAME !== cacheName && cacheName.startsWith("gih-cache") ) { // Check if a cache's name is not the same as the current cache name and its name starts with 'gih-cache': |
|
return caches.delete(cacheName); // Delete that cache. |
|
} |
|
}) |
|
); |
|
}) |
|
); |
|
}); |
|
|
|
|
|
/* |
|
| -------------------------------------------------------------------------- |
|
| Network first and cache if fail |
|
| -------------------------------------------------------------------------- |
|
| |
|
*/ |
|
self.addEventListener("fetch", function(event) { |
|
|
|
if( event.request.includes('jsonplaceholder.typicode.com')){ |
|
|
|
console.log(event.request, '🔥'); |
|
|
|
event.respondWith( |
|
fetch(event.request).catch(function() { |
|
return caches.match(event.request); |
|
}) |
|
); |
|
|
|
} |
|
|
|
}); |