Created
August 2, 2016 09:14
-
-
Save deanhume/3121e51db8727b6cf213c8446e86bb08 to your computer and use it in GitHub Desktop.
Offline SW example
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
function getFilenameFromUrl(path){ | |
path = path.substring(path.lastIndexOf("/")+ 1); | |
return (path.match(/[^.]+(\.[^?#]+)?/) || [])[0]; | |
} | |
this.addEventListener('fetch', event => { | |
if (event.request.mode === 'navigate' || (event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html'))) { | |
event.respondWith( | |
fetch(event.request.url).catch(error => { | |
var cachedFile = getFilenameFromUrl(event.request.url); | |
// Return the offline page | |
return caches.match(cachedFile); | |
}) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment