Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Created March 10, 2019 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreasvirkus/604920df34f95793991f98daff46273d to your computer and use it in GitHub Desktop.
Save andreasvirkus/604920df34f95793991f98daff46273d to your computer and use it in GitHub Desktop.
Service worker with progressive caching
'use strict'
const version = 'v1::'
const offlineFundamentals = [
'/assets/css/variables.css',
'/assets/css/global.css',
'/assets/js/script.min.js',
'/index.html',
// ...
]
self.addEventListener('install', function(event) {
event.waitUntil(
caches
.open(version + 'fundamentals')
.then(function(cache) {
return cache.addAll(offlineFundamentals);
})
.then(function() {
console.log('worker::installed');
})
);
});
self.addEventListener('fetch', function(event) {
const ignoreSearch = event.request.url.indexOf('?') != -1
console.log('worker::fetching');
if (event.request.method !== 'GET') {
console.log('worker::fetch ignored |', event.request.method, event.request.url);
return;
}
event.respondWith(
caches
.match(event.request, { ignoreSearch })
.then(function(cached) {
var networked = fetch(event.request)
.then(fetchedFromNetwork, unableToResolve)
.catch(unableToResolve);
return cached || networked;
function fetchedFromNetwork(response) {
var cacheCopy = response.clone();
caches.open(version + 'pages')
.then(function add(cache) {
cache.put(event.request, cacheCopy);
})
.then(function() {
console.log('worker::fetch cached |', event.request.url);
});
return response;
}
function unableToResolve () {
return new Response('<h1>Service Unavailable</h1>', {
status: 503,
statusText: 'Service Unavailable',
headers: new Headers({
'Content-Type': 'text/html'
})
});
}
})
);
});
self.addEventListener('activate', function(event) {
event.waitUntil(
caches.keys()
.then(function (keys) {
return Promise.all(
keys
.filter(function (key) {
return !key.startsWith(version);
})
.map(function (key) {
return caches.delete(key);
})
);
})
.then(function() {
console.log('worker::activated');
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment