Created
June 22, 2017 00:07
-
-
Save nandomoreirame/a4e81947c2a10508a988e63c4a9dc5c6 to your computer and use it in GitHub Desktop.
Data cache with service worker
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
| (function () { | |
| 'use strict'; | |
| if ('serviceWorker' in navigator) { | |
| navigator.serviceWorker | |
| .register('/assets/javascripts/service-worker.js') | |
| .then(function (registration) { | |
| console.info('Service Worker Registered'); | |
| }).catch(function (e) { | |
| console.error('Error during service worker registration:', e); | |
| }); | |
| } | |
| })(); |
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
| /* eslint no-unused-vars: 0 */ | |
| var dataCacheName = 'serviceWorker-v1'; | |
| var cacheName = 'serviceWorker-cacheName-1'; | |
| var filesToCache = [ | |
| '/assets/javascripts/jquery.js', | |
| '/assets/javascripts/main.js', | |
| '/assets/stylesheets/main.css', | |
| '/assets/images/logo.png', | |
| '/assets/images/loader.gif', | |
| '/assets/images/sprite.png' | |
| ]; | |
| self.addEventListener('install', function (event) { | |
| console.info('[ServiceWorker] Install'); | |
| event.waitUntil( | |
| caches.open(cacheName).then(function (cache) { | |
| console.info('[ServiceWorker] Caching app shell'); | |
| return cache.addAll(filesToCache); | |
| }) | |
| ); | |
| }); | |
| self.addEventListener('activate', function (event) { | |
| console.info('[ServiceWorker] Activate'); | |
| event.waitUntil( | |
| caches.keys().then(function (keyList) { | |
| return Promise.all(keyList.map(function (key) { | |
| if (key !== cacheName && key !== dataCacheName) { | |
| console.info('[ServiceWorker] Removing old cache', key); | |
| return caches.delete(key); | |
| } | |
| })); | |
| }) | |
| ); | |
| return self.clients.claim(); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment