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
This commit is contained in:
kolaente
2026-04-08 10:19:26 +02:00
committed by kolaente
parent 71378fd0b2
commit 10e7d2532e
2 changed files with 9 additions and 1 deletions

View File

@@ -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({

View File

@@ -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<string, string>) {
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',