diff --git a/examples/preact-router/package.json b/examples/preact-router/package.json index 58686711..bca64cec 100644 --- a/examples/preact-router/package.json +++ b/examples/preact-router/package.json @@ -54,6 +54,7 @@ "devDependencies": { "@preact/preset-vite": "^2.7.0", "@rollup/plugin-replace": "^5.0.5", + "@vite-pwa/workbox-window": "^8.0.0", "https-localhost": "^4.7.1", "rimraf": "^5.0.5", "typescript": "^5.2.2", @@ -61,7 +62,6 @@ "vite-plugin-pwa": "workspace:*", "workbox-core": "^7.0.0", "workbox-precaching": "^7.0.0", - "workbox-routing": "^7.0.0", - "workbox-window": "^7.0.0" + "workbox-routing": "^7.0.0" } } diff --git a/examples/react-router/package.json b/examples/react-router/package.json index 9bb2135a..8140d085 100644 --- a/examples/react-router/package.json +++ b/examples/react-router/package.json @@ -60,6 +60,7 @@ "@types/react-dom": "^18.2.15", "@types/react-router-config": "^5.0.10", "@types/react-router-dom": "^5.3.3", + "@vite-pwa/workbox-window": "^8.0.0", "@vitejs/plugin-react": "^4.2.0", "https-localhost": "^4.7.1", "rimraf": "^5.0.5", @@ -68,7 +69,6 @@ "vite-plugin-pwa": "workspace:*", "workbox-core": "^7.0.0", "workbox-precaching": "^7.0.0", - "workbox-routing": "^7.0.0", - "workbox-window": "^7.0.0" + "workbox-routing": "^7.0.0" } } diff --git a/examples/solid-router/package.json b/examples/solid-router/package.json index 56ffdf2c..419f9f54 100644 --- a/examples/solid-router/package.json +++ b/examples/solid-router/package.json @@ -53,6 +53,7 @@ }, "devDependencies": { "@rollup/plugin-replace": "^5.0.5", + "@vite-pwa/workbox-window": "^8.0.0", "https-localhost": "^4.7.1", "rimraf": "^5.0.5", "typescript": "^5.2.2", @@ -61,7 +62,6 @@ "vite-plugin-solid": "^2.7.2", "workbox-core": "^7.0.0", "workbox-precaching": "^7.0.0", - "workbox-routing": "^7.0.0", - "workbox-window": "^7.0.0" + "workbox-routing": "^7.0.0" } } diff --git a/examples/svelte-routify/package.json b/examples/svelte-routify/package.json index ec6ade3c..526daae1 100644 --- a/examples/svelte-routify/package.json +++ b/examples/svelte-routify/package.json @@ -52,6 +52,7 @@ "@roxi/routify": "^2.18.12", "@sveltejs/vite-plugin-svelte": "^3.0.0", "@tsconfig/svelte": "^5.0.2", + "@vite-pwa/workbox-window": "^8.0.0", "eslint": "^8.54.0", "eslint-plugin-svelte3": "^4.0.0", "https-localhost": "^4.7.1", diff --git a/examples/vanilla-ts-no-ip/package.json b/examples/vanilla-ts-no-ip/package.json index bb9a2d62..0e63dbb2 100644 --- a/examples/vanilla-ts-no-ip/package.json +++ b/examples/vanilla-ts-no-ip/package.json @@ -16,6 +16,7 @@ "test": "nr test-generate-sw && nr test-custom-sw" }, "devDependencies": { + "@vite-pwa/workbox-window": "^8.0.0", "lodash-es": "^4.17.21", "rimraf": "^5.0.5", "typescript": "^5.2.2", diff --git a/examples/vue-router/package.json b/examples/vue-router/package.json index fee4b772..3d673ba6 100644 --- a/examples/vue-router/package.json +++ b/examples/vue-router/package.json @@ -53,6 +53,7 @@ }, "devDependencies": { "@rollup/plugin-replace": "^5.0.5", + "@vite-pwa/workbox-window": "^8.0.0", "@vitejs/plugin-vue": "^4.5.0", "@vueuse/core": "^10.6.1", "https-localhost": "^4.7.1", @@ -62,7 +63,6 @@ "vite-plugin-pwa": "workspace:*", "workbox-core": "^7.0.0", "workbox-precaching": "^7.0.0", - "workbox-routing": "^7.0.0", - "workbox-window": "^7.0.0" + "workbox-routing": "^7.0.0" } } diff --git a/examples/vue-router/src/ReloadPrompt.vue b/examples/vue-router/src/ReloadPrompt.vue index 4b60d2b6..5e5a12cd 100644 --- a/examples/vue-router/src/ReloadPrompt.vue +++ b/examples/vue-router/src/ReloadPrompt.vue @@ -25,6 +25,12 @@ const { console.log(`SW Registered: ${r}`) } }, + onInstalling(state) { + console.log(`SW Installing: ${state}`) + }, + onUpdateFound(state) { + console.log(`SW Updating: ${state}`) + }, }) async function close() { diff --git a/package.json b/package.json index 03b56d53..ed232a8f 100644 --- a/package.json +++ b/package.json @@ -106,14 +106,14 @@ "peerDependencies": { "vite": "^3.1.0 || ^4.0.0 || ^5.0.0", "workbox-build": "^7.0.0", - "workbox-window": "^7.0.0" + "@vite-pwa/workbox-window": "^8.0.0" }, "dependencies": { "debug": "^4.3.4", "fast-glob": "^3.3.2", "pretty-bytes": "^6.1.1", "workbox-build": "^7.0.0", - "workbox-window": "^7.0.0" + "@vite-pwa/workbox-window": "^8.0.0" }, "devDependencies": { "@antfu/eslint-config": "^2.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 293db3ad..e8820e6d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,6 +8,9 @@ importers: .: dependencies: + '@vite-pwa/workbox-window': + specifier: ^8.0.0 + version: 8.0.0 debug: specifier: ^4.3.4 version: 4.3.4 @@ -20,9 +23,6 @@ importers: workbox-build: specifier: ^7.0.0 version: 7.0.0 - workbox-window: - specifier: ^7.0.0 - version: 7.0.0 devDependencies: '@antfu/eslint-config': specifier: ^2.0.0 @@ -167,6 +167,9 @@ importers: '@rollup/plugin-replace': specifier: ^5.0.5 version: 5.0.5(rollup@4.4.1) + '@vite-pwa/workbox-window': + specifier: ^8.0.0 + version: 8.0.0 https-localhost: specifier: ^4.7.1 version: 4.7.1 @@ -191,9 +194,6 @@ importers: workbox-routing: specifier: ^7.0.0 version: 7.0.0 - workbox-window: - specifier: ^7.0.0 - version: 7.0.0 examples/react-router: dependencies: @@ -228,6 +228,9 @@ importers: '@types/react-router-dom': specifier: ^5.3.3 version: 5.3.3 + '@vite-pwa/workbox-window': + specifier: ^8.0.0 + version: 8.0.0 '@vitejs/plugin-react': specifier: ^4.2.0 version: 4.2.0(vite@5.0.0) @@ -255,9 +258,6 @@ importers: workbox-routing: specifier: ^7.0.0 version: 7.0.0 - workbox-window: - specifier: ^7.0.0 - version: 7.0.0 examples/solid-router: dependencies: @@ -271,6 +271,9 @@ importers: '@rollup/plugin-replace': specifier: ^5.0.5 version: 5.0.5(rollup@4.4.1) + '@vite-pwa/workbox-window': + specifier: ^8.0.0 + version: 8.0.0 https-localhost: specifier: ^4.7.1 version: 4.7.1 @@ -298,9 +301,6 @@ importers: workbox-routing: specifier: ^7.0.0 version: 7.0.0 - workbox-window: - specifier: ^7.0.0 - version: 7.0.0 examples/svelte-routify: devDependencies: @@ -316,6 +316,9 @@ importers: '@tsconfig/svelte': specifier: ^5.0.2 version: 5.0.2 + '@vite-pwa/workbox-window': + specifier: ^8.0.0 + version: 8.0.0 eslint: specifier: ^8.54.0 version: 8.54.0 @@ -436,6 +439,9 @@ importers: examples/vanilla-ts-no-ip: devDependencies: + '@vite-pwa/workbox-window': + specifier: ^8.0.0 + version: 8.0.0 lodash-es: specifier: ^4.17.21 version: 4.17.21 @@ -510,6 +516,9 @@ importers: '@rollup/plugin-replace': specifier: ^5.0.5 version: 5.0.5(rollup@4.4.1) + '@vite-pwa/workbox-window': + specifier: ^8.0.0 + version: 8.0.0 '@vitejs/plugin-vue': specifier: ^4.5.0 version: 4.5.0(vite@5.0.0)(vue@3.3.8) @@ -540,9 +549,6 @@ importers: workbox-routing: specifier: ^7.0.0 version: 7.0.0 - workbox-window: - specifier: ^7.0.0 - version: 7.0.0 packages: @@ -4174,6 +4180,9 @@ packages: vite: 3.1.0 dev: true + /@vite-pwa/workbox-window@8.0.0: + resolution: {integrity: sha512-FLM90ZQTGp5u/ZobN9U7c440PwtrqThIsJwTGuJPtbQm5HqKiFKkPMpE7P1HKG4az1izTcg+e4HLpJCKOP5Usw==} + /@vitejs/plugin-react@4.2.0(vite@5.0.0): resolution: {integrity: sha512-+MHTH/e6H12kRp5HUkzOGqPMksezRMmW+TNzlh/QXfI8rRf6l2Z2yH/v12no1UvTwhZgEDMuQ7g7rrfMseU6FQ==} engines: {node: ^14.18.0 || >=16.0.0} diff --git a/preact.d.ts b/preact.d.ts index 334e04fe..765392fe 100644 --- a/preact.d.ts +++ b/preact.d.ts @@ -9,6 +9,8 @@ declare module 'virtual:pwa-register/preact' { export function useRegisterSW(options?: RegisterSWOptions): { needRefresh: [boolean, StateUpdater] offlineReady: [boolean, StateUpdater] + installingSW: [boolean, StateUpdater] + updatingSW: [boolean, StateUpdater] /** * Reloads the current window to allow the service worker take the control. * diff --git a/react.d.ts b/react.d.ts index 500b355c..ec36475f 100644 --- a/react.d.ts +++ b/react.d.ts @@ -9,6 +9,8 @@ declare module 'virtual:pwa-register/react' { export function useRegisterSW(options?: RegisterSWOptions): { needRefresh: [boolean, Dispatch>] offlineReady: [boolean, Dispatch>] + installingSW: [boolean, Dispatch>] + updatingSW: [boolean, Dispatch>] /** * Reloads the current window to allow the service worker take the control. * diff --git a/solid.d.ts b/solid.d.ts index 47c173d4..97af8a5a 100644 --- a/solid.d.ts +++ b/solid.d.ts @@ -9,6 +9,8 @@ declare module 'virtual:pwa-register/solid' { export function useRegisterSW(options?: RegisterSWOptions): { needRefresh: [Accessor, Setter] offlineReady: [Accessor, Setter] + installingSW: [Accessor, Setter] + updatingSW: [Accessor, Setter] /** * Reloads the current window to allow the service worker take the control. * diff --git a/src/client/build/preact.ts b/src/client/build/preact.ts index 847817e8..3cb74eca 100644 --- a/src/client/build/preact.ts +++ b/src/client/build/preact.ts @@ -12,10 +12,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { onRegistered, onRegisteredSW, onRegisterError, + onInstalling, + onUpdateFound, } = options const [needRefresh, setNeedRefresh] = useState(false) const [offlineReady, setOfflineReady] = useState(false) + const [installingSW, setInstallingSW] = useState(false) + const [updatingSW, setUpdatingSW] = useState(false) const [updateServiceWorker] = useState(() => { return registerSW({ @@ -28,6 +32,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { setNeedRefresh(true) onNeedRefresh?.() }, + onInstalling(state, sw) { + setInstallingSW(state) + onInstalling?.(state, sw) + }, + onUpdateFound(state, sw) { + setUpdatingSW(state) + onUpdateFound?.(state, sw) + }, onRegistered, onRegisteredSW, onRegisterError, @@ -37,6 +49,8 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { return { needRefresh: [needRefresh, setNeedRefresh], offlineReady: [offlineReady, setOfflineReady], + installingSW: [installingSW, setInstallingSW], + updatingSW: [updatingSW, setUpdatingSW], updateServiceWorker, } } diff --git a/src/client/build/react.ts b/src/client/build/react.ts index b0105819..c9f65323 100644 --- a/src/client/build/react.ts +++ b/src/client/build/react.ts @@ -12,10 +12,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { onRegistered, onRegisteredSW, onRegisterError, + onInstalling, + onUpdateFound, } = options const [needRefresh, setNeedRefresh] = useState(false) const [offlineReady, setOfflineReady] = useState(false) + const [installingSW, setInstallingSW] = useState(false) + const [updatingSW, setUpdatingSW] = useState(false) const [updateServiceWorker] = useState(() => { return registerSW({ @@ -28,6 +32,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { setNeedRefresh(true) onNeedRefresh?.() }, + onInstalling(state, sw) { + setInstallingSW(state) + onInstalling?.(state, sw) + }, + onUpdateFound(state, sw) { + setUpdatingSW(state) + onUpdateFound?.(state, sw) + }, onRegistered, onRegisteredSW, onRegisterError, @@ -37,6 +49,8 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { return { needRefresh: [needRefresh, setNeedRefresh], offlineReady: [offlineReady, setOfflineReady], + installingSW: [installingSW, setInstallingSW], + updatingSW: [updatingSW, setUpdatingSW], updateServiceWorker, } } diff --git a/src/client/build/register.ts b/src/client/build/register.ts index 5a27f0fe..6883acb5 100644 --- a/src/client/build/register.ts +++ b/src/client/build/register.ts @@ -22,9 +22,11 @@ export function registerSW(options: RegisterSWOptions = {}) { onRegistered, onRegisteredSW, onRegisterError, + onInstalling, + onUpdateFound, } = options - let wb: import('workbox-window').Workbox | undefined + let wb: import('@vite-pwa/workbox-window').Workbox | undefined let registerPromise: Promise let sendSkipWaitingMessage: () => Promise | undefined @@ -37,7 +39,7 @@ export function registerSW(options: RegisterSWOptions = {}) { async function register() { if ('serviceWorker' in navigator) { - wb = await import('workbox-window').then(({ Workbox }) => { + wb = await import('@vite-pwa/workbox-window').then(({ Workbox }) => { // __SW__, __SCOPE__ and __TYPE__ will be replaced by virtual module return new Workbox('__SW__', { scope: '__SCOPE__', type: '__TYPE__' }) }).catch((e) => { @@ -57,19 +59,26 @@ export function registerSW(options: RegisterSWOptions = {}) { } if (!autoDestroy) { if (auto) { + wb.addEventListener('installing', (event) => { + event.isUpdate === true || event.isExternal === true + ? onUpdateFound?.(true, event.sw) + : onInstalling?.(true, event.sw) + }) wb.addEventListener('activated', (event) => { if (event.isUpdate || event.isExternal) window.location.reload() }) wb.addEventListener('installed', (event) => { - if (event.isUpdate === false) { + event.isUpdate || event.isExternal + ? onUpdateFound?.(false, event.sw) + : onInstalling?.(false, event.sw) + if (event.isUpdate === false) onOfflineReady?.() - } }); } else { let onNeedRefreshCalled = false - const showSkipWaitingPrompt = (event?: import('workbox-window').WorkboxLifecycleWaitingEvent) => { + const showSkipWaitingPrompt = (event?: import('@vite-pwa/workbox-window').WorkboxLifecycleWaitingEvent) => { /* FIX: - open page in a new tab and navigate to home page @@ -100,7 +109,16 @@ export function registerSW(options: RegisterSWOptions = {}) { onNeedRefresh?.() } + wb.addEventListener('installing', (event) => { + event.isUpdate === true || event.isExternal === true + ? onUpdateFound?.(true, event.sw) + : onInstalling?.(true, event.sw) + }) wb.addEventListener('installed', (event) => { + event.isUpdate === true || event.isExternal === true + ? onUpdateFound?.(false, event.sw) + : onInstalling?.(false, event.sw) + if (typeof event.isUpdate === 'undefined') { if (typeof event.isExternal !== 'undefined') { if (event.isExternal) diff --git a/src/client/build/solid.ts b/src/client/build/solid.ts index 76e378ea..d2d05263 100644 --- a/src/client/build/solid.ts +++ b/src/client/build/solid.ts @@ -12,10 +12,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { onRegistered, onRegisteredSW, onRegisterError, + onInstalling, + onUpdateFound, } = options const [needRefresh, setNeedRefresh] = createSignal(false) const [offlineReady, setOfflineReady] = createSignal(false) + const [installingSW, setInstallingSW] = createSignal(false) + const [updatingSW, setUpdatingSW] = createSignal(false) const updateServiceWorker = registerSW({ immediate, @@ -27,6 +31,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { setNeedRefresh(true) onNeedRefresh?.() }, + onInstalling(state, sw) { + setInstallingSW(state) + onInstalling?.(state, sw) + }, + onUpdateFound(state, sw) { + setUpdatingSW(state) + onUpdateFound?.(state, sw) + }, onRegistered, onRegisteredSW, onRegisterError, @@ -35,6 +47,8 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { return { needRefresh: [needRefresh, setNeedRefresh], offlineReady: [offlineReady, setOfflineReady], + installingSW: [installingSW, setInstallingSW], + updatingSW: [updatingSW, setUpdatingSW], updateServiceWorker, } } diff --git a/src/client/build/svelte.ts b/src/client/build/svelte.ts index f1e71cc1..44d38f34 100644 --- a/src/client/build/svelte.ts +++ b/src/client/build/svelte.ts @@ -12,10 +12,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { onRegistered, onRegisteredSW, onRegisterError, + onInstalling, + onUpdateFound, } = options const needRefresh = writable(false) const offlineReady = writable(false) + const installingSW = writable(false) + const updatingSW = writable(false) const updateServiceWorker = registerSW({ immediate, @@ -27,6 +31,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { needRefresh.set(true) onNeedRefresh?.() }, + onInstalling(state, sw) { + installingSW.set(state) + onInstalling?.(state, sw) + }, + onUpdateFound(state, sw) { + updatingSW.set(state) + onUpdateFound?.(state, sw) + }, onRegistered, onRegisteredSW, onRegisterError, @@ -35,6 +47,8 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { return { needRefresh, offlineReady, + installingSW, + updatingSW, updateServiceWorker, } } diff --git a/src/client/build/vue.ts b/src/client/build/vue.ts index 4fd6801a..d936939a 100644 --- a/src/client/build/vue.ts +++ b/src/client/build/vue.ts @@ -12,10 +12,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { onRegistered, onRegisteredSW, onRegisterError, + onInstalling, + onUpdateFound, } = options const needRefresh = ref(false) const offlineReady = ref(false) + const installingSW = ref(false) + const updatingSW = ref(false) const updateServiceWorker = registerSW({ immediate, @@ -27,6 +31,14 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { offlineReady.value = true onOfflineReady?.() }, + onInstalling(state, sw) { + installingSW.value = state + onInstalling?.(state, sw) + }, + onUpdateFound(state, sw) { + updatingSW.value = state + onUpdateFound?.(state, sw) + }, onRegistered, onRegisteredSW, onRegisterError, @@ -36,5 +48,7 @@ export function useRegisterSW(options: RegisterSWOptions = {}) { updateServiceWorker, offlineReady, needRefresh, + installingSW, + updatingSW, } } diff --git a/src/client/dev/preact.ts b/src/client/dev/preact.ts index 1ff78db9..167acf8b 100644 --- a/src/client/dev/preact.ts +++ b/src/client/dev/preact.ts @@ -6,12 +6,16 @@ export type { RegisterSWOptions } export function useRegisterSW(_options: RegisterSWOptions = {}) { const needRefresh = useState(false) const offlineReady = useState(false) + const installingSW = useState(false) + const updatingSW = useState(false) const updateServiceWorker = (_reloadPage?: boolean) => {} return { needRefresh, offlineReady, + installingSW, + updatingSW, updateServiceWorker, } } diff --git a/src/client/dev/react.ts b/src/client/dev/react.ts index e97d21c9..7735764e 100644 --- a/src/client/dev/react.ts +++ b/src/client/dev/react.ts @@ -6,12 +6,16 @@ export type { RegisterSWOptions } export function useRegisterSW(_options: RegisterSWOptions = {}) { const needRefresh = useState(false) const offlineReady = useState(false) + const installingSW = useState(false) + const updatingSW = useState(false) const updateServiceWorker = (_reloadPage?: boolean) => {} return { needRefresh, offlineReady, + installingSW, + updatingSW, updateServiceWorker, } } diff --git a/src/client/dev/solid.ts b/src/client/dev/solid.ts index 29b9ce84..05da0308 100644 --- a/src/client/dev/solid.ts +++ b/src/client/dev/solid.ts @@ -6,12 +6,16 @@ export type { RegisterSWOptions } export function useRegisterSW(_options: RegisterSWOptions = {}) { const needRefresh = createSignal(false) const offlineReady = createSignal(false) + const installingSW = createSignal(false) + const updatingSW = createSignal(false) const updateServiceWorker = (_reloadPage?: boolean) => {} return { needRefresh, offlineReady, + installingSW, + updatingSW, updateServiceWorker, } } diff --git a/src/client/dev/svelte.ts b/src/client/dev/svelte.ts index 5e60019f..a763c7bb 100644 --- a/src/client/dev/svelte.ts +++ b/src/client/dev/svelte.ts @@ -6,12 +6,16 @@ export type { RegisterSWOptions } export function useRegisterSW(_options: RegisterSWOptions = {}) { const needRefresh = writable(false) const offlineReady = writable(false) + const installingSW = writable(false) + const updatingSW = writable(false) const updateServiceWorker = (_reloadPage?: boolean) => {} return { needRefresh, offlineReady, + installingSW, + updatingSW, updateServiceWorker, } } diff --git a/src/client/dev/vue.ts b/src/client/dev/vue.ts index 40ddbdd8..9a4b2748 100644 --- a/src/client/dev/vue.ts +++ b/src/client/dev/vue.ts @@ -6,6 +6,8 @@ export type { RegisterSWOptions } export function useRegisterSW(_options: RegisterSWOptions = {}) { const needRefresh = ref(false) const offlineReady = ref(false) + const installingSW = ref(false) + const updatingSW = ref(false) const updateServiceWorker = (_reloadPage?: boolean) => {} @@ -13,5 +15,7 @@ export function useRegisterSW(_options: RegisterSWOptions = {}) { updateServiceWorker, offlineReady, needRefresh, + installingSW, + updatingSW, } } diff --git a/src/client/type.d.ts b/src/client/type.d.ts index fcb2a563..680bf62a 100644 --- a/src/client/type.d.ts +++ b/src/client/type.d.ts @@ -17,4 +17,26 @@ export interface RegisterSWOptions { */ onRegisteredSW?: (swScriptUrl: string, registration: ServiceWorkerRegistration | undefined) => void onRegisterError?: (error: any) => void + /** + * Called when the service worker is installing for the first time. + * + * The service worker's `installing` event is transient: + * - this callback is called when the service worker is installing for the first time + * - once the service worker has been installed, the callback is called again with state `false` + * + * @param state true when the service worker is installing for first time and false when installed. + * @param sw The service worker instance. + */ + onInstalling?: (state: boolean, sw?: ServiceWorker) => void + /** + * Called when a new service worker version is found and it is installing. + * + * The service worker's `installing` event is transient: + * - this callback is called when a new service worker version has been detected + * - once the new service worker has been installed, the callback is called again with state `false` + * + * @param state true when the service worker is installing and false when installed. + * @param sw The service worker instance. + */ + onUpdateFound?: (state: boolean, sw?: ServiceWorker) => void } diff --git a/src/plugins/main.ts b/src/plugins/main.ts index 9d9f6914..e1dc3f38 100644 --- a/src/plugins/main.ts +++ b/src/plugins/main.ts @@ -1,4 +1,4 @@ -import type { Plugin, UserConfig } from 'vite' +import type { Plugin } from 'vite' import { VIRTUAL_MODULES, VIRTUAL_MODULES_MAP, @@ -14,14 +14,6 @@ export function MainPlugin(ctx: PWAPluginContext, api: VitePluginPWAAPI) { return { name: 'vite-plugin-pwa', enforce: 'pre', - config() { - return { - ssr: { - // TODO: remove until workbox-window support native ESM - noExternal: ['workbox-window'], - }, - } - }, async configResolved(config) { ctx.useImportRegister = false ctx.viteConfig = config diff --git a/svelte.d.ts b/svelte.d.ts index b0227bcd..90e105de 100644 --- a/svelte.d.ts +++ b/svelte.d.ts @@ -9,6 +9,8 @@ declare module 'virtual:pwa-register/svelte' { export function useRegisterSW(options?: RegisterSWOptions): { needRefresh: Writable offlineReady: Writable + installingSW: Writable + updatingSW: Writable /** * Reloads the current window to allow the service worker take the control. * diff --git a/types/index.d.ts b/types/index.d.ts index fcb2a563..680bf62a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -17,4 +17,26 @@ export interface RegisterSWOptions { */ onRegisteredSW?: (swScriptUrl: string, registration: ServiceWorkerRegistration | undefined) => void onRegisterError?: (error: any) => void + /** + * Called when the service worker is installing for the first time. + * + * The service worker's `installing` event is transient: + * - this callback is called when the service worker is installing for the first time + * - once the service worker has been installed, the callback is called again with state `false` + * + * @param state true when the service worker is installing for first time and false when installed. + * @param sw The service worker instance. + */ + onInstalling?: (state: boolean, sw?: ServiceWorker) => void + /** + * Called when a new service worker version is found and it is installing. + * + * The service worker's `installing` event is transient: + * - this callback is called when a new service worker version has been detected + * - once the new service worker has been installed, the callback is called again with state `false` + * + * @param state true when the service worker is installing and false when installed. + * @param sw The service worker instance. + */ + onUpdateFound?: (state: boolean, sw?: ServiceWorker) => void } diff --git a/vue.d.ts b/vue.d.ts index 278c98e8..aa28539a 100644 --- a/vue.d.ts +++ b/vue.d.ts @@ -9,6 +9,8 @@ declare module 'virtual:pwa-register/vue' { export function useRegisterSW(options?: RegisterSWOptions): { needRefresh: Ref offlineReady: Ref + installingSW: Ref + updatingSW: Ref /** * Reloads the current window to allow the service worker take the control. *