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) {
52
52
53
53
54
54
function 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
+
55
75
for ( const entry of options . runtimeCaching ) {
56
76
const urlPattern = new RegExp ( entry . urlPattern )
57
77
const method = entry . method || 'GET'
58
78
59
79
const plugins = ( entry . strategyPlugins || [ ] )
60
80
. map ( p => new ( getProp ( workbox , p . use ) ) ( ...p . config ) )
61
81
82
+ plugins . unshift ( requestInterceptor )
83
+
62
84
const strategyOptions = { ...entry . strategyOptions , plugins }
63
85
64
86
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) {
156
156
157
157
158
158
function 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
+
159
179
for (const entry of options.runtimeCaching) {
160
180
const urlPattern = new RegExp (entry .urlPattern )
161
181
const method = entry .method || ' GET'
162
182
163
183
const plugins = (entry .strategyPlugins || [])
164
184
.map (p => new (getProp (workbox , p .use ))(... p .config ))
165
185
186
+ plugins .unshift (requestInterceptor )
187
+
166
188
const strategyOptions = { ... entry .strategyOptions , plugins }
167
189
168
190
const strategy = new workbox .strategies [entry .handler ](strategyOptions )
You can’t perform that action at this time.
0 commit comments