]> git.ardis.lu - ardislu.dev/commitdiff
Immediately replace old service workers
authorArdis Lu <ardisglu@gmail.com>
Wed, 25 Sep 2024 04:04:06 +0000 (21:04 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 25 Sep 2024 04:04:06 +0000 (21:04 -0700)
sw.js

diff --git a/sw.js b/sw.js
index 78b73a44687a22f5ee2b0c0925554980c2ca78bd..1e7c991cf6151521682575877b88468086bda6a6 100644 (file)
--- a/sw.js
+++ b/sw.js
@@ -27,7 +27,7 @@ async function revalidate(request) {
 
 // Implements stale-while-revalidate strategy: if there's anything in the cache, return that immediately.
 // Wait for the origin server's response in parallel and notify cacheChannel if the cache changed.
-globalThis.addEventListener('fetch', event => {
+addEventListener('fetch', event => {
   if (event.request.method !== 'GET') {
     return;
   }
@@ -37,3 +37,6 @@ globalThis.addEventListener('fetch', event => {
     return cacheResponse === undefined ? originResponse : cacheResponse;
   }));
 });
+
+addEventListener('install', skipWaiting); // Immediately override any pre-existing service worker
+addEventListener('activate', () => clients.claim()); // Immediately intercept fetch requests after first registration without page reload
This page took 0.038296 seconds and 4 git commands to generate.