File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -52,13 +52,35 @@ function precacheAssets(workbox, options) {
5252
5353
5454function runtimeCaching ( workbox , options ) {
55+ const requestInterceptor = {
56+ requestWillFetch ( { request } ) {
57+ if ( request . cache === 'only-if-cached' && request . mode === 'no-cors' ) {
58+ return new Request ( request . url , { ...request , cache : 'default' , mode : 'no-cors' } )
59+ }
60+ return request
61+ } ,
62+ fetchDidFail ( ctx ) {
63+ ctx . error . message =
64+ '[workbox] Network request for ' + ctx . request . url + ' threw an error: ' + ctx . error . message
65+ console . error ( ctx . error , 'Details:' , ctx )
66+ } ,
67+ handlerDidError ( ctx ) {
68+ ctx . error . message =
69+ `[workbox] Network handler threw an error: ` + ctx . error . message
70+ console . error ( ctx . error , 'Details:' , ctx )
71+ return null
72+ }
73+ }
74+
5575 for ( const entry of options . runtimeCaching ) {
5676 const urlPattern = new RegExp ( entry . urlPattern )
5777 const method = entry . method || 'GET'
5878
5979 const plugins = ( entry . strategyPlugins || [ ] )
6080 . map ( p => new ( getProp ( workbox , p . use ) ) ( ...p . config ) )
6181
82+ plugins . unshift ( requestInterceptor )
83+
6284 const strategyOptions = { ...entry . strategyOptions , plugins }
6385
6486 const strategy = new workbox . strategies [ entry . handler ] ( strategyOptions )
Original file line number Diff line number Diff line change @@ -156,13 +156,35 @@ function precacheAssets(workbox, options) {
156156
157157
158158function runtimeCaching(workbox, options) {
159+ const requestInterceptor = {
160+ requestWillFetch({ request }) {
161+ if (request .cache === ' only-if-cached' && request .mode === ' no-cors' ) {
162+ return new Request (request .url , { ... request , cache: ' default' , mode: ' no-cors' })
163+ }
164+ return request
165+ },
166+ fetchDidFail(ctx ) {
167+ ctx .error .message =
168+ ' [workbox] Network request for ' + ctx .request .url + ' threw an error: ' + ctx .error .message
169+ console .error (ctx .error , ' Details:' , ctx )
170+ },
171+ handlerDidError(ctx ) {
172+ ctx .error .message =
173+ \` [workbox] Network handler threw an error: \` + ctx.error.message
174+ console.error(ctx.error, 'Details:', ctx)
175+ return null
176+ }
177+ }
178+
159179 for (const entry of options.runtimeCaching) {
160180 const urlPattern = new RegExp (entry .urlPattern )
161181 const method = entry .method || ' GET'
162182
163183 const plugins = (entry .strategyPlugins || [])
164184 .map (p => new (getProp (workbox , p .use ))(... p .config ))
165185
186+ plugins .unshift (requestInterceptor )
187+
166188 const strategyOptions = { ... entry .strategyOptions , plugins }
167189
168190 const strategy = new workbox .strategies [entry .handler ](strategyOptions )
You can’t perform that action at this time.
0 commit comments