From 937383f6daf03053fd9ca7716ca69831c21c4e35 Mon Sep 17 00:00:00 2001 From: "Robert (Jamie) Munro" Date: Fri, 6 Feb 2026 11:45:43 +0000 Subject: [PATCH] fix: Change hardcoded SW_VERSION to use dynamic build version Don't edit files that get committed to git. --- client/package.json | 7 +++---- client/scripts/update-sw-version.js | 19 ------------------- client/src/lib/sw-version.js | 2 +- client/src/service-worker.js | 2 +- client/vite.config.js | 9 ++++++++- 5 files changed, 13 insertions(+), 26 deletions(-) delete mode 100644 client/scripts/update-sw-version.js diff --git a/client/package.json b/client/package.json index 6603d0b..2ecdea4 100644 --- a/client/package.json +++ b/client/package.json @@ -3,10 +3,9 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "npm run update-sw-version && vite", - "build": "npm run update-sw-version && vite build --emptyOutDir", - "preview": "vite preview", - "update-sw-version": "node scripts/update-sw-version.js" + "dev": "vite", + "build": "vite build --emptyOutDir", + "preview": "vite preview" }, "devDependencies": { "@sofie-automation/code-standard-preset": "^2.5.2", diff --git a/client/scripts/update-sw-version.js b/client/scripts/update-sw-version.js deleted file mode 100644 index f10be87..0000000 --- a/client/scripts/update-sw-version.js +++ /dev/null @@ -1,19 +0,0 @@ -import * as fs from 'fs' - -/* - This script updates a constant in two places. - This is used for the main application to detect if the service worker is outdated. - This script is intended to be run upon each build. -*/ - -const files = ['src/service-worker.js', 'src/lib/sw-version.js'] - -const version = new Date().toISOString() -for (const file of files) { - const content = await fs.promises.readFile(file, 'utf-8') - - const newContent = content.replace(/SW_VERSION = '[^']+'/, `SW_VERSION = '${version}'`) - - await fs.promises.writeFile(file, newContent) -} -console.log(`Updated SW_VERSION to ${version}`) diff --git a/client/src/lib/sw-version.js b/client/src/lib/sw-version.js index e3a2904..830d298 100644 --- a/client/src/lib/sw-version.js +++ b/client/src/lib/sw-version.js @@ -1 +1 @@ -export const SW_VERSION = '2026-02-03T14:13:28.664Z' // Updated at build time +export const SW_VERSION = '__BUILD_VERSION__' // Generated at build time diff --git a/client/src/service-worker.js b/client/src/service-worker.js index 443ff7a..069ed28 100644 --- a/client/src/service-worker.js +++ b/client/src/service-worker.js @@ -1,4 +1,4 @@ -const SW_VERSION = '2026-02-03T14:13:28.664Z' // Updated at build time +const SW_VERSION = '__BUILD_VERSION__' // Generated at build time let requestId = 0 const requestMap = new Map() diff --git a/client/vite.config.js b/client/vite.config.js index ab5f464..d05663f 100644 --- a/client/vite.config.js +++ b/client/vite.config.js @@ -4,7 +4,8 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills' import { defineConfig } from 'vite' import { buildSync } from 'esbuild' -// import react from '@vitejs/plugin-react' +// Generate version once for entire build to ensure sync between main bundle and service worker +const BUILD_VERSION = new Date().toISOString() export default defineConfig({ root: path.resolve(__dirname, 'src'), @@ -28,6 +29,9 @@ export default defineConfig({ outDir: '../dist', }, base: '/', + define: { + '__BUILD_VERSION__': JSON.stringify(BUILD_VERSION), + }, plugins: [ // ... @@ -42,6 +46,9 @@ export default defineConfig({ bundle: true, entryPoints: [path.join(process.cwd(), 'src/service-worker.js')], outfile: path.join(process.cwd(), 'dist/service-worker.js'), + define: { + '__BUILD_VERSION__': JSON.stringify(BUILD_VERSION), + }, }) }, },