Skip to content

Commit f20489c

Browse files
authored
fix(workbox): add additional details for uncaught errors and fix chromium CORS (#417)
1 parent be2bc76 commit f20489c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

templates/workbox/sw.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,35 @@ function precacheAssets(workbox, options) {
5252

5353

5454
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+
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)

test/__snapshots__/pwa.test.js.snap

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,35 @@ function precacheAssets(workbox, options) {
156156
157157
158158
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+
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)

0 commit comments

Comments
 (0)