Created
May 24, 2018 09:36
-
-
Save Antoinebr/28e09cbb8e58bad7417e705af8809a80 to your computer and use it in GitHub Desktop.
SW CAST
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
caches.open('my-pwa-demo') | |
var CACHE_NAME = "my-pwa-demo"; | |
var CACHED_URLS = [ | |
'/', | |
"index.html", | |
"app.js", | |
"app.css" | |
]; | |
self.addEventListener("install", function(event) { | |
event.waitUntil( | |
caches.open(CACHE_NAME).then(function(cache) { // we open or create the cache | |
return cache.addAll(CACHED_URLS); // we put all urls contained in CACHED_URL in the cache | |
}) | |
); | |
}); | |
self.addEventListener("fetch", function(event) { | |
console.log("Fetch request for:", event.request.url); | |
}); | |
self.addEventListener("fetch", function(event) { | |
console.log(event.request.url); | |
event.respondWith( | |
fetch(event.request).catch(function() { // if the fetch fail | |
return caches.match(event.request).then(function(response) { | |
if (response) { // match will allways resolve() we have to check for the response | |
return response; | |
} | |
// if there's no response and the request is "text/html" we return index-offline.html | |
else if (event.request.headers.get("accept").includes("text/html") ){ | |
return caches.match("/index-offline.html"); | |
} | |
}); | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment