Last active
February 6, 2018 13:35
-
-
Save nhiroki/c1dff7eecbea86b4f761 to your computer and use it in GitHub Desktop.
Distinguish between Document and ServiceWorkerGlobalScope
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> | |
<head> | |
<title>Hello, world!</title> | |
</head> | |
<body> | |
<script src="sw.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 ('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