Created
May 15, 2019 17:37
-
-
Save alex-titarenko/9527e80f03556df63f4008e1291143b6 to your computer and use it in GitHub Desktop.
PWA Blog Post - Code snippets (serviceworker.js)
This file contains hidden or 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
// "Install" event handler will be invoked only once when you first time navigate to the page | |
self.addEventListener('install', function(event) { | |
event.waitUntil( | |
caches.open('app-cache').then(function(cache) { | |
// Adds index.html page to the cache | |
return cache.add('index.html'); | |
}) | |
); | |
}); | |
// Will be invoked for every network call: XHR, static resources, etc. | |
self.addEventListener('fetch', function(event) { | |
event.respondWith( | |
// For every request checks matching value in the cache | |
caches.match(event.request).then(function(response) { | |
// Returns cached response; otherwise, makes an actual network call | |
return response || fetch(event.request); | |
}) | |
); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment