Skip to content

Instantly share code, notes, and snippets.

@leon0707
Created February 4, 2020 14:59
Show Gist options
  • Select an option

  • Save leon0707/393bbdc6a73b9a298b7d928bea228f20 to your computer and use it in GitHub Desktop.

Select an option

Save leon0707/393bbdc6a73b9a298b7d928bea228f20 to your computer and use it in GitHub Desktop.
cache worker
var CACHE_NAME = 'my-site-cache-v1';
var urlsToCache = [
'/',
'/styles/main.css',
'/script/main.js'
];
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
});
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
// Cache hit - return response
if (response) {
return response;
}
return fetch(event.request);
}
)
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment