]> git.ardis.lu - ardislu.dev/commitdiff
Only cache requests on own hostname
authorArdis Lu <ardisglu@gmail.com>
Wed, 25 Sep 2024 04:15:03 +0000 (21:15 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 25 Sep 2024 04:15:03 +0000 (21:15 -0700)
Otherwise it will try to cache chrome extensions or user-initiated requests from console, which was not intended.

sw.js

diff --git a/sw.js b/sw.js
index 1e7c991cf6151521682575877b88468086bda6a6..1e6463cbe85a3f77cd4eac54e8a928ee06c4e8fc 100644 (file)
--- a/sw.js
+++ b/sw.js
@@ -1,4 +1,4 @@
-const cacheName = 'v5';
+const cacheName = 'v6';
 const cacheChannel = new BroadcastChannel('cache');
 
 async function revalidate(request) {
@@ -28,7 +28,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.
 addEventListener('fetch', event => {
-  if (event.request.method !== 'GET') {
+  if (event.request.method !== 'GET' || new URL(event.request.url).hostname !== location.hostname) {
     return;
   }
 
This page took 0.035016 seconds and 4 git commands to generate.