Skip to content

Instantly share code, notes, and snippets.

@jakearchibald
Last active January 15, 2016 20:21
Show Gist options
  • Save jakearchibald/8d9a6100c47521b5ed88 to your computer and use it in GitHub Desktop.
Save jakearchibald/8d9a6100c47521b5ed88 to your computer and use it in GitHub Desktop.
<!doctype html>
<script>
navigator.serviceWorker.addEventListener('controllerchange', () => window.location.reload());
navigator.serviceWorker.register('sw.js');
if (navigator.serviceWorker.controller) {
var req = new XMLHttpRequest();
req.open('GET', 'xhr');
req.onload = function() {
console.log('XHR loaded', req.responseText);
};
// Handle network errors
req.onerror = function() {
console.log('XHR error');
};
// Make the request
req.send();
}
</script>
self.addEventListener('activate', () => clients.claim());
self.addEventListener('fetch', event => {
if (event.request.url.endsWith('xhr')) {
event.respondWith(new Response('This is from the service worker'));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment