Last active
June 13, 2016 20:44
-
-
Save jeffposnick/bc055528be84f0dc2c5ed1b7191f3320 to your computer and use it in GitHub Desktop.
SW test
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
| <html> | |
| <head> | |
| <title>SW Test</title> | |
| </head> | |
| <body> | |
| <img src="nope.jpg"> | |
| <script> | |
| navigator.serviceWorker.register('sw.js'); | |
| </script> | |
| </body> | |
| </html> |
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
| '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