From b3350c8918cc2013abcd47ca4a0cc77f97189e05 Mon Sep 17 00:00:00 2001 From: Christoph Nakazawa Date: Thu, 25 Jan 2024 06:17:08 +0900 Subject: [PATCH] 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 --- src/client/build/register.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/client/build/register.ts b/src/client/build/register.ts index c404513c..2fad68b7 100644 --- a/src/client/build/register.ts +++ b/src/client/build/register.ts @@ -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.