Skip to content

Instantly share code, notes, and snippets.

@alex-titarenko
Created May 15, 2019 17:37
Show Gist options
  • Save alex-titarenko/9527e80f03556df63f4008e1291143b6 to your computer and use it in GitHub Desktop.
Save alex-titarenko/9527e80f03556df63f4008e1291143b6 to your computer and use it in GitHub Desktop.
PWA Blog Post - Code snippets (serviceworker.js)
// "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