I'm reading the ServiceWorker getting started docs .
So first we register our ServiceWorker with the scope. So I did that as below
<!DOCTYPE html>
<html>
<head>
<title>Getting started with Service Worker</title>
</head>
<body>
<script type="text/javascript">
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
</script>
</body>
</html>
and created a sw.js file and wrote a console.log inside sw.js
console.log("This is sw.js")
So when I access the index.html file the first time, that console.log is executed and ServiceWorker registration successful message is printed. But when I refresh the page second time, it only prints the message ServiceWorker registration successful. So why didn't it executed the console.log inside sw.js for the second time. I was expecting it to execute that console.log second time. Am I missing the point here?
I'm reading the ServiceWorker getting started docs https://developers.google./web/fundamentals/getting-started/primers/service-workers.
So first we register our ServiceWorker with the scope. So I did that as below
<!DOCTYPE html>
<html>
<head>
<title>Getting started with Service Worker</title>
</head>
<body>
<script type="text/javascript">
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
</script>
</body>
</html>
and created a sw.js file and wrote a console.log inside sw.js
console.log("This is sw.js")
So when I access the index.html file the first time, that console.log is executed and ServiceWorker registration successful message is printed. But when I refresh the page second time, it only prints the message ServiceWorker registration successful. So why didn't it executed the console.log inside sw.js for the second time. I was expecting it to execute that console.log second time. Am I missing the point here?
Share Improve this question asked Jun 5, 2017 at 8:08 Mahtab AlamMahtab Alam 1,8703 gold badges23 silver badges42 bronze badges 1- Just FWIW, sometimes people get service workers and general-purpose web workers confused. Are you sure you want a service worker for what you're doing? Service workers are a special kind of web worker used for managing page resources (for instance, offline apps). – T.J. Crowder Commented Jun 5, 2017 at 8:57
2 Answers
Reset to default 4Service workers are scripts that are basically executed once (in background) when they are registered. If you need to execute some recurrent code, you need to use service worker API:
A service worker is an event-driven worker registered against an origin and a path.
The install event is called asynchronously after you call navigator.serviceWorker.register('/sw.js')
; that's why it returns a Promise, to which you pass a callback
function (the one with the content console.log('ServiceWorker registration successful ...');
).
A service worker is designed to sit in between your website and the network. So while you load your page initially, it runs the script once and installs the service worker if you have added an event to listen for installs.
Once that's done, you need to attach a listener to the 'fetch' event to capture all your requests inside your page. So basically if you want to console.log every time a request is made, you can do the following:
self.addEventListener('fetch', event => {
console.log(event);
})
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745091056a4610696.html
评论列表(0条)