[PWA] 2. Service Worker Life Cycle

Source: Internet
Author: User

    1. Once serive worker is registered, the first time we go to the app, we cannot see the logs from servcie works. Any only refersh it second time, then we able to see the logs.
    2. Once we change service worker, it doesn't seem that we have change it.

 

The no1. is because, service worker only take control after it is loaded. but the first time we go to the page, service worker actually is not there, because, the browser need to fetch our assets, CSS, and JavaScript fiels which include our SW. JS. so that's why the first time we go to page, we didn't see the logs. but after we refresh the page, the SW. JS is already cached. so the service worker can control from now on.

 

The NO2. is because, once we change the code, it will check with service worker and if it is changed, it will become next version of serive worker.ButIt doesn' t take control, itWatis. It won't take control unitl all page use current version are gone. And a refresh doens't help, it because a overlap bewteen window client.Only you close the current page or naviagte to a page that isn't controlled by current service worker. When it does that, the new version of servie worker will take control.

 

So if you change the code a little bit:

Self. addeventlistener ('Fetch', Function (Event) {Console. Log ("Yo",Event. Request );});

 

And then close the current page, then open it again, you will see the new logs come out:

 

[PWA] 2. Service Worker Life Cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.