@@ -28,7 +28,7 @@ self.addEventListener('activate', (event) => {
2828 self . clients . claim ( ) ;
2929} ) ;
3030
31- // Fetch event: Stale-While-Revalidate strategy
31+ // Fetch event: Strategy-based resource handling
3232self . addEventListener ( 'fetch' , ( event ) => {
3333 const url = new URL ( event . request . url ) ;
3434
@@ -42,13 +42,20 @@ self.addEventListener('fetch', (event) => {
4242 // These are typically documents being edited, which should always be fresh.
4343 if ( url . searchParams . has ( 'file' ) || url . searchParams . has ( 'src' ) ) return ;
4444
45- // 4. Stale-While-Revalidate Strategy
46- event . respondWith (
47- caches . match ( event . request ) . then ( ( cachedResponse ) => {
48- const fetchPromise = fetch ( event . request )
45+ // 4. Determine Strategy
46+ const isHtml = event . request . mode === 'navigate' ||
47+ url . pathname . endsWith ( '.html' ) ||
48+ url . pathname === '/' ||
49+ url . pathname . endsWith ( '/' ) ;
50+
51+ if ( isHtml ) {
52+ // Strategy: Network-First for HTML/Navigation
53+ // Ensuring the user always gets the latest version if online,
54+ // but can still access the app when offline.
55+ event . respondWith (
56+ fetch ( event . request )
4957 . then ( ( networkResponse ) => {
50- // Only cache valid 200 responses
51- if ( networkResponse && networkResponse . status === 200 && networkResponse . type === 'basic' ) {
58+ if ( networkResponse && networkResponse . status === 200 ) {
5259 const responseToCache = networkResponse . clone ( ) ;
5360 caches . open ( CACHE_NAME ) . then ( ( cache ) => {
5461 cache . put ( event . request , responseToCache ) ;
@@ -57,15 +64,34 @@ self.addEventListener('fetch', (event) => {
5764 return networkResponse ;
5865 } )
5966 . catch ( ( ) => {
60- // If network fails, return the cached version if we have it
61- return cachedResponse ;
62- } ) ;
67+ return caches . match ( event . request ) ;
68+ } )
69+ ) ;
70+ } else {
71+ // Strategy: Stale-While-Revalidate for other static assets (JS, CSS, Images)
72+ event . respondWith (
73+ caches . match ( event . request ) . then ( ( cachedResponse ) => {
74+ const fetchPromise = fetch ( event . request )
75+ . then ( ( networkResponse ) => {
76+ // Only cache valid 200 responses
77+ if ( networkResponse && networkResponse . status === 200 && networkResponse . type === 'basic' ) {
78+ const responseToCache = networkResponse . clone ( ) ;
79+ caches . open ( CACHE_NAME ) . then ( ( cache ) => {
80+ cache . put ( event . request , responseToCache ) ;
81+ } ) ;
82+ }
83+ return networkResponse ;
84+ } )
85+ . catch ( ( ) => {
86+ return cachedResponse ;
87+ } ) ;
6388
64- // Use event.waitUntil to ensure the fetch/cache update continues in background
65- if ( ! cachedResponse ) {
66- return fetchPromise ;
67- }
68- return cachedResponse ;
69- } ) ,
70- ) ;
89+ // Use event.waitUntil to ensure the fetch/cache update continues in background
90+ if ( ! cachedResponse ) {
91+ return fetchPromise ;
92+ }
93+ return cachedResponse ;
94+ } ) ,
95+ ) ;
96+ }
7197} ) ;
0 commit comments