Service Workers - waitUntil, skipWaiting, and claim methods

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 ก.ย. 2024
  • This is the third video in the Understanding Service Workers Series.
    This video focuses on three very powerful methods that you can use inside Service workers to control the timing of the installation and activation of the service worker files - ev.waitUntil, self.skipWaiting, and clients.claim.
    The waitUntil method we will be using in future videos about the cache api and the fetch api too.
    Code from video: gist.github.co...
    Images from picsum:
    picsum.photos/...
    picsum.photos/...
    MDN reference for waitUntil: developer.mozi...
    MDN reference for skipWaiting: developer.mozi...
    MDN reference for claim: developer.mozi...
    Full playlist about service workers: • Service Workers - Regi...

ความคิดเห็น • 27

  • @leolowe22
    @leolowe22 5 หลายเดือนก่อน

    Thanks for the video one question though, After we claimed we still did not get fetch request interception from the updated service worker after the claim message was printed.

  • @vidhyarevathi9281
    @vidhyarevathi9281 3 ปีที่แล้ว +2

    Great explanation. I surfed a lot but this helped me

  • @alighadiri6123
    @alighadiri6123 3 ปีที่แล้ว +2

    I was so confused about update lifecycle of serviceWorkers. now I know everything thanks to you. didn't find an in-depth tutorial like this.♥

  • @tabliqatchi6696
    @tabliqatchi6696 ปีที่แล้ว +1

    I've got two questions:
    1 - At 10:50 you say if we've made breaking changes we must be careful about using Clients.claim. May I know like what sort of changes would be considered breaking here?
    2 - Also at 12:17, skipWaiting is still uncommented, and when you refresh, the stuff in the install event handler don't run at all, why? What skipWaiting does is skipping the waiting to activation (of a new installation), why would it cause the other code in the install handler not to run?
    Thank you this video is great ♥

  • @shujaatali8414
    @shujaatali8414 หลายเดือนก่อน

    I really loved to your videos.

  • @derkvanderveen8938
    @derkvanderveen8938 3 ปีที่แล้ว +2

    Great, crystal clear.

  • @АлександрКостарев-ж1к
    @АлександрКостарев-ж1к 2 ปีที่แล้ว

    Didn't regret watching this =)
    But my sw "fetch" listener did not work if I press "clean caches and hard reset" button in chrome. Nor waitUntil() nor skipWainting() nor claim() does not help with that.
    Though claim() helped if I unregister sw in dev tools and reload.

  • @iwswordpress
    @iwswordpress 3 ปีที่แล้ว +1

    Thanks for this. SW lifecycles can be quite involved. Good explanations.

    • @StijnHommes
      @StijnHommes 3 ปีที่แล้ว

      There is only one thing that matters: their life cycle is too long. The life cycle of a service worker should be zero seconds (or less).

  • @KarimShaloh
    @KarimShaloh 3 ปีที่แล้ว +1

    Link for code gist is not included

  • @tomekk3569
    @tomekk3569 ปีที่แล้ว

    Great explanation! Thank you very much. :)

  • @wallurisatya6986
    @wallurisatya6986 3 ปีที่แล้ว

    awesome explanation. Big fan of your tutorials.

  • @xyz-ey7ul
    @xyz-ey7ul 2 ปีที่แล้ว

    What's the purpose of the "controllerChange" event?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 ปีที่แล้ว +1

      Because a service worker can be controlling multiple windows/tabs and your code can force a new service worker to take over the pages, this gives us an event to listen for.

  • @expukpuk
    @expukpuk 3 ปีที่แล้ว

    Thanks Mate, nice.

  • @kingwindie
    @kingwindie 3 ปีที่แล้ว

    so the self.skipWaiting will not be triggered until the .then() chains are completed? Promise is kinda confusing to me

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  3 ปีที่แล้ว

      Here is my playlist about Promises to help you - th-cam.com/video/SmPouEFKOBg/w-d-xo.html

    • @kingwindie
      @kingwindie 3 ปีที่แล้ว +1

      @@SteveGriffith-Prof3ssorSt3v3 thank you so much, really helpful videos

  • @omerdavid5490
    @omerdavid5490 2 ปีที่แล้ว

    Hey! the video is awsome.
    Do you know any way to listen "fetch" and *delay* or *block* the http request of file?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 ปีที่แล้ว

      Inside the fetch listener and its the waitUntil( ) you can do whatever you want with the request. You can return a different file, fetch a new one from a server, send an old version from the cache and then fetch a new version from the server and put that in the cache...

    • @omerdavid5490
      @omerdavid5490 2 ปีที่แล้ว

      @@SteveGriffith-Prof3ssorSt3v3 Its ok to do it with event.respondWith and then in the callback, delay the request?

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 ปีที่แล้ว

      @@omerdavid5490 Sorry, I meant respondWith inside the fetch event handler function. You can't create a delay though because the service worker wants to stop running.

    • @omerdavid5490
      @omerdavid5490 2 ปีที่แล้ว

      @@SteveGriffith-Prof3ssorSt3v3 what do u mean? i tried this and its looks like its work for me . can you please explain?
      event.respondWith(
      (async function () {
      await waitTimeout(10000);
      return await fetch(event.request);
      })()
      );

    • @SteveGriffith-Prof3ssorSt3v3
      @SteveGriffith-Prof3ssorSt3v3  2 ปีที่แล้ว

      @@omerdavid5490 there is only so long the browser will wait before canceling the request. That goes for the service worker too.

  • @mod7ex_fx
    @mod7ex_fx ปีที่แล้ว