Skip to content

Instantly share code, notes, and snippets.

@kevzlou7979
Created November 13, 2017 06:40
Show Gist options
  • Save kevzlou7979/5e073751c5700aa7874ed11962e9f238 to your computer and use it in GitHub Desktop.
Save kevzlou7979/5e073751c5700aa7874ed11962e9f238 to your computer and use it in GitHub Desktop.
service-worker.js
var cacheName = 'cache_1510547879365';
var filesToCache = [
'GwtMaterialDesign/3AA7DDB3C56AA598E88D6C7AB1AFEF5E.cache.js',
'GwtMaterialDesign/GwtMaterialDesign.devmode.js',
'GwtMaterialDesign/GwtMaterialDesign.nocache.js',
'GwtMaterialDesign/clear.cache.gif',
'GwtMaterialDesign/css/animation.css',
'GwtMaterialDesign/css/animation.min.css',
'GwtMaterialDesign/css/material-icons.css',
'GwtMaterialDesign/css/materialize.blue.css',
'GwtMaterialDesign/css/materialize.blue.min.css',
'GwtMaterialDesign/css/materialize.css',
'GwtMaterialDesign/css/materialize.min.css',
'GwtMaterialDesign/css/overridecss.blue.css',
'GwtMaterialDesign/css/overridecss.blue.min.css',
'GwtMaterialDesign/css/overridecss.css',
'GwtMaterialDesign/css/overridecss.min.css',
'index.html',
'launcher-icons/launcher1x.png',
'launcher-icons/launcher2x.png',
'launcher-icons/launcher4x.png',
'splash/splash.css',
'splash/splash.js'
];
self.addEventListener('install', function (e) {
console.log('[ServiceWorker] Install');
e.waitUntil(
caches.open(cacheName).then(function (cache) {
console.log('[ServiceWorker] Caching app shell');
return cache.addAll(filesToCache);
})
);
});
self.addEventListener('activate', function (e) {
console.log('[ServiceWorker] Activate');
e.waitUntil(
caches.keys().then(function (keyList) {
return Promise.all(keyList.map(function (key) {
console.log('[ServiceWorker] Removing old cache', key);
if (key !== cacheName) {
return caches.delete(key);
}
}));
})
);
});
self.addEventListener('fetch', function (e) {
console.log('[ServiceWorker] Fetch', e.request.url);
e.respondWith(
caches.match(e.request).then(function (response) {
return response || fetch(e.request);
})
);
});
self.addEventListener('message', function (e) {
if (e.data == 'skipWaiting') {
self.skipWaiting();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment