javascript - Understanding ServiceWorker registration - Stack Overflow

I'm reading the ServiceWorker getting started docs .So first we register our ServiceWorker with th

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
Add a ment  | 

2 Answers 2

Reset to default 4

Service 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

相关推荐

  • javascript - Understanding ServiceWorker registration - Stack Overflow

    I'm reading the ServiceWorker getting started docs .So first we register our ServiceWorker with th

    12小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信