Last active
May 11, 2017 19:28
-
-
Save bholtbholt/48a1edf8fd5490a32e6b53a4a13664c6 to your computer and use it in GitHub Desktop.
Service Workers Example
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Server A</title> | |
<link href='https://fonts.googleapis.com/css?family=Ravi+Prakash' rel='stylesheet' type='text/css'> | |
<style> | |
p { | |
font-family: 'Ravi Prakash', serif; | |
font-size: 6em; | |
} | |
</style> | |
</head> | |
<body> | |
<p>Server A</p> | |
<script src="registersw.js"></script> | |
<script src="http://localhost:8080/exjs.js"></script> | |
<script src="https://unpkg.com/react@15.5.4/dist/react.js"></script> | |
</body> | |
</html> |
This file contains 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
if ('serviceWorker' in navigator) { | |
navigator.serviceWorker | |
.register('./sw.js') | |
.then(registration => console.log('ServiceWorker registration successful with scope: ', registration.scope)) | |
.catch(err => console.log("Service Worker Failed to Register", err)) | |
} |
This file contains 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
// https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API/Using_Service_Workers | |
// https://mobiforge.com/design-development/taking-web-offline-service-workers | |
// https://www.youtube.com/watch?v=BfL3pprhnms | |
// chrome://serviceworker-internals/ | |
const CACHE_NAME = "V1" | |
const preCache = [ | |
'./', | |
'./index.html', | |
'http://localhost:8080/exjs.js', | |
'https://unpkg.com/react@15.5.4/dist/react.js', | |
'https://fonts.googleapis.com/css?family=Ravi+Prakash' | |
] | |
const OFFLINE_FILE = '/index.html' | |
self.addEventListener('install', event => { | |
event.waitUntil( | |
caches.open(CACHE_NAME) | |
.then(cache => cache.addAll(preCache)) | |
.catch(cache => console.log("ERROROROROR", cache)) | |
); | |
}); | |
self.addEventListener('fetch', event => { | |
event.respondWith( | |
caches.match(event.request) | |
.then(response => response || fetch(event.request)) | |
.catch(() => caches.match(OFFLINE_FILE)) | |
) | |
}) | |
self.addEventListener('activate', event => { | |
event.waitUntil( | |
// Version our caches by removing old ones | |
caches.keys().then(cacheKeys => { | |
return Promise.all( | |
cacheKeys | |
.filter(cacheName => (cacheName !== CACHE_NAME)) | |
.map(cacheName => caches.delete(cacheName)) | |
) | |
}) | |
); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment