Skip to content

Commit 9aa76f8

Browse files
author
pooya parsa
committed
feat: improve sw.register error handling
1 parent cd6c9cc commit 9aa76f8

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

packages/workbox/templates/sw.register.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
export default async function (ctx, inject) {
1+
function onError(error) {<% if (options.dev) { %>console.error('Error registering workbox:', error) <% } %>}
2+
3+
export default function (ctx, inject) {
24
let workbox = {}
35

46
try {
7+
// workbox-window does not detects unsupported browsers
58
if (!'serviceWorker' in navigator) {
6-
throw new Error('Serviceworker is not supported!')
9+
throw new Error('serviceWorker is not supported in current browser!')
710
}
811

12+
// Use require() instead of import() to prevent creating extra chunk
13+
// Use es5 version to prevent crashing older browsers while parsing bundle
914
const { Workbox } = require('workbox-cdn/workbox/workbox-window.<%= options.dev ? 'dev' : 'prod' %>.es5.mjs')
1015

1116
workbox = new Workbox('<%= options.swURL %>', {
1217
scope: '<%= options.swScope %>'
1318
})
1419

15-
workbox.register()
16-
} catch (e) {
17-
console.warn('Cannot register workbox:', e)
20+
workbox.register().catch(onError)
21+
} catch (error) {
22+
onError(error)
1823
}
1924

2025
// Inject as $workbox

0 commit comments

Comments
 (0)