Skip to content

Commit b3350c8

Browse files
cpojeruserquin
andauthored
feat: add error handling when importing workbox-window. (#646)
* Add error handling to workbox import. * chore: refactor importing workbox window logic --------- Co-authored-by: userquin <[email protected]>
1 parent f35a687 commit b3350c8

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/client/build/register.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,17 @@ export function registerSW(options: RegisterSWOptions = {}) {
3737

3838
async function register() {
3939
if ('serviceWorker' in navigator) {
40-
const { Workbox } = await import('workbox-window')
41-
// __SW__, __SCOPE__ and __TYPE__ will be replaced by virtual module
42-
wb = new Workbox('__SW__', { scope: '__SCOPE__', type: '__TYPE__' })
40+
wb = await import('workbox-window').then(({ Workbox }) => {
41+
// __SW__, __SCOPE__ and __TYPE__ will be replaced by virtual module
42+
return new Workbox('__SW__', { scope: '__SCOPE__', type: '__TYPE__' })
43+
}).catch((e) => {
44+
onRegisterError?.(e)
45+
return undefined
46+
})
47+
48+
if (!wb)
49+
return
50+
4351
sendSkipWaitingMessage = async () => {
4452
// Send a message to the waiting service worker,
4553
// instructing it to activate.

0 commit comments

Comments
 (0)