Skip to content

Instantly share code, notes, and snippets.

@michalstocki
Last active February 4, 2016 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalstocki/d9c5ce8da134a9b24ea0 to your computer and use it in GitHub Desktop.
Save michalstocki/d9c5ce8da134a9b24ea0 to your computer and use it in GitHub Desktop.
Service Worker CORS Test
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Service Worker Test</title>
</head>
<body>
<headset>
<h1>Test of registering service worker</h1>
<h2>from different sub domain</h2>
</headset>
<p>Registration: <span id="register"></span></p>
<p>States: <ol id="states"></ol></p>
<p>Are service workers available: <span id="availability"></span></p>
<p>There is currently a service worker handling requests on this page for the current navigation: <span id="controlled"></span></p>
<p>Activity kind: <span id="kind"></span></p>
<script>
function logState(state) {
var liElement = document.createElement('li');
liElement.textContent = state;
document.querySelector('#states').appendChild(liElement);
}
if ('serviceWorker' in navigator) {
// The current browser supports service workers.
document.querySelector('#availability').textContent = '✔︎';
// navigator.serviceWorker.controlled iff there is currently a service worker handling
// requests on this page for the current navigation.
document.querySelector('#controlled').textContent = navigator.serviceWorker.controller ? '✔︎' : '✗';
// Override the default scope of '/' with './', so that the registration applies
// to the current directory and everything underneath it.
navigator.serviceWorker.register('https://googlechrome.github.io/samples/service-worker/registration-events/service-worker.js', {scope: './'}).then(function(registration) {
// At this point, registration has taken place.
// The service worker will not handle requests until this page and any
// other instances of this page (in other tabs, etc.) have been
// closed/reloaded.
document.querySelector('#register').textContent = 'succeeded';
var serviceWorker;
if (registration.installing) {
serviceWorker = registration.installing;
document.querySelector('#kind').textContent = 'installing';
} else if (registration.waiting) {
serviceWorker = registration.waiting;
document.querySelector('#kind').textContent = 'waiting';
} else if (registration.active) {
serviceWorker = registration.active;
document.querySelector('#kind').textContent = 'active';
}
if (serviceWorker) {
logState(serviceWorker.state);
serviceWorker.addEventListener('statechange', function(e) {
logState(e.target.state);
});
}
}).catch(function(error) {
// Something went wrong during registration. The service-worker.js file
// might be unavailable or contain a syntax error.
document.querySelector('#register').textContent = 'failed: ' + error;
});
} else {
// The current browser doesn't support service workers.
document.querySelector('#availability').textContent = 'are not';
}
</script>
</body>
</html>
/*
Copyright 2014 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// In Chrome, you can view these log messages and many more useful pieces of
// debugging info at chrome://inspect/#service-workers
self.addEventListener('install', function(e) {
// This event will be fired once when this version of the script is first registered for
// a given URL scope.
// It's an opportunity to initialize caches and prefetch data, if desired. This sort of
// work should be wrapped in a Promise, and e.waitUntil(promise) can be used to ensure that
// this installation does not complete until the Promise is settled.
// Also, be aware that there may already be an existing service worker controlling the page
// (either an earlier version of this script or a completely different script.)
console.log('Install event:', e);
});
self.addEventListener('activate', function(e) {
// This event will be fired once when this version of the script is first registered for
// a given URL scope.
// It's an opportunity to clean up any stale data that might be left behind in self.caches
// by an older version of this script.
// e.waitUntil(promise) is also available here to delay activation until work has been performed,
// but note that waiting within the activate event will delay handling of any
// fetch or message events that are fired in the interim. When possible, do work during the install phase.
// It will NOT be fired each time the service worker is revived after being terminated.
// To perform an action when the service worker is revived, include that logic in the
// `onfetch` or `onmessage` event listeners.
console.log('Activate event:', e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment