Service worker is caching files but fetch event is never fired

后端 未结 5 1826
独厮守ぢ
独厮守ぢ 2020-12-01 07:14

I have just attempted to implement service workers to cache some JSON files and other assets on a static site (running on localhost chrome Version 47.0.2526.73 (64-bit)). Us

5条回答
  •  感动是毒
    2020-12-01 08:01

    I struggled with this for a long time, and I think the documentation related to the matter is seriously lacking. In my experience, there is a very important distinction:

    The service worker can only intercept fetch events if it is in or above the scope of the URL it is accessed from.

    For example, my sw.js file was located at /static/sw.js. When accessing my site's root at / and attempting to intercept fetch events to js files in /static/js/common.js, the fetch events were not intercepted, even though the scope of my service worker was /static/ and the js file was in /static/js/.

    Once I moved my sw.js file to the top-level scope /sw.js, the fetch events were all intercepted. This is because the scope of the page I was accessing with my browser / was the same as the scope of my sw.js file /.

    Please let me know if this clears things up for people, or if I am incorrect!

提交回复
热议问题