Skip to content

Instantly share code, notes, and snippets.

@nhiroki
Last active February 6, 2018 13:35
Show Gist options
  • Save nhiroki/c1dff7eecbea86b4f761 to your computer and use it in GitHub Desktop.
Save nhiroki/c1dff7eecbea86b4f761 to your computer and use it in GitHub Desktop.
Distinguish between Document and ServiceWorkerGlobalScope
<!DOCTYPE html>
<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<script src="sw.js"></script>
</body>
</html>
if ('ServiceWorkerGlobalScope' in self &&
self instanceof ServiceWorkerGlobalScope) {
self.addEventListener('fetch', function(e) {
console.log(e.request.url);
});
} else {
navigator.serviceWorker.register("sw.js", {scope: "./scope/"})
.then(function(reg) {
console.log(reg.scope);
})
.catch(function(e) {
console.log(e);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment