<html>
<title>create service worker</title>
<body>this page is for registering a service worker</body>
<script>
// Copy of //content/test/data/service_worker/create_service_worker.html.
// Simulates navigator.serviceWorker.ready, which can't be used since this
// document may not be in-scope.
async function whenReady(registration) {
if (registration.active)
return;
// If there's no .active, .waiting will activate before .installing.
const nextActiveWorker = registration.waiting || registration.installing;
return new Promise((resolve, reject) => {
nextActiveWorker.addEventListener('statechange', event => {
if (nextActiveWorker.state == 'activated')
resolve();
if (nextActiveWorker.state == 'redundant')
reject('worker became redundant');
});
});
}
async function register(worker_url, scope, type) {
try {
const init = {};
if (scope)
init['scope'] = scope;
if (type)
init['type'] = type;
const registration =
await navigator.serviceWorker.register(worker_url, init);
await whenReady(registration);
return 'DONE';
} catch (error) {
return `${error}`;
}
}
async function update(scope) {
try {
const registration =
scope ?
await navigator.serviceWorker.getRegistration(scope) :
await navigator.serviceWorker.ready;
await registration.update();
return 'DONE';
} catch (error) {
return `${error}`;
}
}
</script>
</html>