Skip to content

Instantly share code, notes, and snippets.

@TyrealGray
Created April 6, 2019 21:31
Show Gist options
  • Save TyrealGray/72691bcfeeef8f48a5eb6841f413710c to your computer and use it in GitHub Desktop.
Save TyrealGray/72691bcfeeef8f48a5eb6841f413710c to your computer and use it in GitHub Desktop.
service-worker.js example
'use strict';

const CACHE_VERSION = 1;
let CURRENT_CACHES = {
	offline: 'hiddout-v' + CACHE_VERSION,
};

self.addEventListener('install', (event) => {
	console.log('The service worker is being installed.');
	event.waitUntil(
		caches.open(CURRENT_CACHES.offline).then(function(cache) {
			return cache.addAll([
				'/public/index.html',
				'/public/static/Hiddout.png',
			]);
		}),
	);
});

self.addEventListener('activate', function(event) {
	let expectedCacheNames = Object.keys(CURRENT_CACHES).map(function(key) {
		return CURRENT_CACHES[key];
	});

	event.waitUntil(
		caches.keys().then((cacheNames) => {
			return Promise.all(
				cacheNames.map((cacheName) => {
					if (expectedCacheNames.indexOf(cacheName) === -1) {
						console.log('Deleting out of date cache:', cacheName);
						return caches.delete(cacheName);
					}
				}),
			);
		}),
	);
});

self.addEventListener('fetch', function(event) {
	event.respondWith(
		caches.open(CURRENT_CACHES.offline).then(function(cache) {
			return fetch(event.request)
				.then(function(response) {
					cache.put(event.request, response.clone());
					return response;
				})
				.catch(() =>
					cache.match(event.request).then(function(response) {
						return response || caches.match('/public/index.html');
					}),
				);
		}),
	);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment