From 10e7d2532ea060606b30a69eb2a954a0fb8f645c Mon Sep 17 00:00:00 2001 From: kolaente Date: Wed, 8 Apr 2026 10:19:26 +0200 Subject: [PATCH] fix: derive workbox version from package.json at build time Instead of hardcoding the workbox version string in the service worker, read it from workbox-precaching/package.json via Vite's define option. This ensures the service worker always references the correct workbox version that is actually installed. Resolves #2549 --- frontend/src/sw.ts | 3 ++- frontend/vite.config.ts | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/frontend/src/sw.ts b/frontend/src/sw.ts index 07c26685f..b23613d4e 100644 --- a/frontend/src/sw.ts +++ b/frontend/src/sw.ts @@ -1,9 +1,10 @@ import {getFullBaseUrl} from './helpers/getFullBaseUrl' declare let self: ServiceWorkerGlobalScope +declare const __WORKBOX_VERSION__: string const fullBaseUrl = getFullBaseUrl() -const workboxVersion = 'v7.3.0' +const workboxVersion = __WORKBOX_VERSION__ importScripts(`${fullBaseUrl}workbox-${workboxVersion}/workbox-sw.js`) workbox.setConfig({ diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index df830027d..46ce0eb22 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -4,6 +4,7 @@ import {configDefaults} from 'vitest/config' import vue from '@vitejs/plugin-vue' import {URL, fileURLToPath} from 'node:url' import {dirname, resolve} from 'node:path' +import {readFileSync} from 'node:fs' import VueI18nPlugin from '@intlify/unplugin-vue-i18n/vite' import {VitePWA} from 'vite-plugin-pwa' @@ -103,8 +104,14 @@ export default defineConfig(({command, mode}) => { }) function getBuildConfig(env: Record) { + const workboxPkgPath = resolve(dirname(pathSrc), 'node_modules/workbox-precaching/package.json') + const workboxVersion = JSON.parse(readFileSync(workboxPkgPath, 'utf-8')).version + return { base: env.VIKUNJA_FRONTEND_BASE, + define: { + __WORKBOX_VERSION__: JSON.stringify(`v${workboxVersion}`), + }, // https://vitest.dev/config/ test: { environment: 'happy-dom',