1- import { registerRoute , Route , setDefaultHandler } from 'workbox-routing' ;
2- import { NetworkOnly , StaleWhileRevalidate } from 'workbox-strategies' ;
1+ import {
2+ NavigationRoute ,
3+ registerRoute ,
4+ Route ,
5+ setCatchHandler ,
6+ setDefaultHandler
7+ } from 'workbox-routing' ;
8+ import { NetworkOnly , StaleWhileRevalidate , Strategy , StrategyHandler } from 'workbox-strategies' ;
39import { ExpirationPlugin } from 'workbox-expiration' ;
4- import { clientsClaim } from "workbox-core" ;
5- import { cleanupOutdatedCaches , precacheAndRoute } from "workbox-precaching" ;
10+ import { cleanupOutdatedCaches , createHandlerBoundToURL , precacheAndRoute } from "workbox-precaching" ;
611
712declare let self : ServiceWorkerGlobalScope & { __WB_DISABLE_DEV_LOGS ?: boolean }
813
14+ self . addEventListener ( 'message' , ( event ) => {
15+ if ( event . data && event . data . type === 'SKIP_WAITING' ) {
16+ self . skipWaiting ( )
17+ }
18+ } )
19+
920
1021// TODO: comment this line if you want to debug workbox (very verbose!) logs in the console
1122self . __WB_DISABLE_DEV_LOGS = true
1223
13- cleanupOutdatedCaches ( )
1424const wbManifest = self . __WB_MANIFEST
1525precacheAndRoute ( wbManifest ) ;
1626
17- setDefaultHandler ( new NetworkOnly ( ) )
27+ cleanupOutdatedCaches ( )
28+
29+ // To allow working offline
30+ registerRoute ( new NavigationRoute (
31+ createHandlerBoundToURL ( 'index.html' )
32+ ) )
1833
1934// A new route that matches same-origin image requests and handles
2035// them with the cache-first, falling back to network strategy:
@@ -32,7 +47,20 @@ const imageRoute = new Route(({ request, sameOrigin }) => {
3247// Register the new route
3348registerRoute ( imageRoute ) ;
3449
35- // this is necessary, since the new service worker will keep on skipWaiting state
36- // and then, caches will not be cleared since it is not activated
37- self . skipWaiting ( )
38- clientsClaim ( )
50+ setDefaultHandler ( new NetworkOnly ( ) )
51+ setCatchHandler ( async ( event ) => {
52+ const headers : Record < string , string > = { } ;
53+ event . request . headers . forEach ( ( v , k ) => { headers [ k ] = v ; } )
54+ console . warn ( `CatchHandler called for: ${ JSON . stringify ( {
55+ destination : event . request . destination ,
56+ url : event . request . url ,
57+ method : event . request . method ,
58+ mode : event . request . mode ,
59+ cache : event . request . cache ,
60+ credentials : event . request . credentials ,
61+ headers,
62+ redirect : event . request . redirect ,
63+ bodyUsed : event . request . bodyUsed
64+ } ) } `)
65+ return Response . error ( ) ;
66+ } )
0 commit comments