Skip to content

Commit

Permalink
feat: add error handling when importing workbox-window. (#646)
Browse files Browse the repository at this point in the history
* Add error handling to workbox import.

* chore: refactor importing workbox window logic

---------

Co-authored-by: userquin <userquin@gmail.com>
  • Loading branch information
cpojer and userquin committed Jan 24, 2024
1 parent f35a687 commit b3350c8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/client/build/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,17 @@ export function registerSW(options: RegisterSWOptions = {}) {

async function register() {
if ('serviceWorker' in navigator) {
const { Workbox } = await import('workbox-window')
// __SW__, __SCOPE__ and __TYPE__ will be replaced by virtual module
wb = new Workbox('__SW__', { scope: '__SCOPE__', type: '__TYPE__' })
wb = await import('workbox-window').then(({ Workbox }) => {
// __SW__, __SCOPE__ and __TYPE__ will be replaced by virtual module
return new Workbox('__SW__', { scope: '__SCOPE__', type: '__TYPE__' })
}).catch((e) => {
onRegisterError?.(e)
return undefined
})

if (!wb)
return

sendSkipWaitingMessage = async () => {
// Send a message to the waiting service worker,
// instructing it to activate.
Expand Down

0 comments on commit b3350c8

Please sign in to comment.