Skip to content

Instantly share code, notes, and snippets.

@jeffposnick
Last active June 13, 2016 20:44
Show Gist options
  • Select an option

  • Save jeffposnick/bc055528be84f0dc2c5ed1b7191f3320 to your computer and use it in GitHub Desktop.

Select an option

Save jeffposnick/bc055528be84f0dc2c5ed1b7191f3320 to your computer and use it in GitHub Desktop.
SW test
<html>
<head>
<title>SW Test</title>
</head>
<body>
<img src="nope.jpg">
<script>
navigator.serviceWorker.register('sw.js');
</script>
</body>
</html>
'use strict';
const CACHE_VERSION = 1;
const CURRENT_CACHES = {
prefetch: 'cache-v' + CACHE_VERSION
};
self.addEventListener('install', e => {
const urlsToPrefetch = [
];
let doPreCaching = caches.open(CURRENT_CACHES.prefetch).then(cache => {
return cache.addAll(urlsToPrefetch);
});
e.waitUntil(self.skipWaiting().then(doPreCaching));
});
self.addEventListener('activate', e => {
e.waitUntil(self.clients.claim());
});
self.addEventListener('fetch', e => {
console.info('Incoming fetch for', e.request);
const requestURL = new URL(e.request.url);
e.respondWith(
caches.open(CURRENT_CACHES.prefetch).then(cache => {
return cache.match(e.request).then(resp => {
// Cache-first strategy.
if (resp) {
return resp; // Cache hit. Respond with cached entry.
}
// Also get a fresh copy from the network.
return fetch(e.request).then(resp => {
console.log('Fetching:', e.request.url)
//cache.put(e.request, resp.clone());
return resp;
}).catch(err => {
console.error('Fetching failed:', err);
throw error;
});
});
})
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment