Skip to content

Change hardcoded SW_VERSION to use dynamic version at build time#10

Open
rjmunro wants to merge 1 commit intomainfrom
rjmunro/improve-version-generation
Open

Change hardcoded SW_VERSION to use dynamic version at build time#10
rjmunro wants to merge 1 commit intomainfrom
rjmunro/improve-version-generation

Conversation

@rjmunro
Copy link

@rjmunro rjmunro commented Feb 6, 2026

Don't edit files that get committed to git.

Don't edit files that get committed to git.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request modernizes the service worker version management by replacing a build-time file modification script with Vite's native define feature. The change eliminates the need to modify committed source files during the build process, using compile-time constant replacement instead.

Changes:

  • Adds __BUILD_VERSION__ constant generation in vite.config.js using current timestamp
  • Configures Vite's define to inject the version into both main bundle and service worker
  • Removes the update-sw-version.js script that previously modified source files
  • Updates package.json scripts to remove dependency on the deleted script

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
client/vite.config.js Generates BUILD_VERSION constant and configures define for both Vite (main bundle) and esbuild (service worker)
client/src/service-worker.js Replaces hardcoded timestamp with BUILD_VERSION placeholder
client/src/lib/sw-version.js Replaces hardcoded timestamp with BUILD_VERSION placeholder
client/scripts/update-sw-version.js Removes obsolete script that modified files at build time
client/package.json Removes references to update-sw-version script from dev and build commands

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 40 to 54
{
apply: 'build',
enforce: 'post',
transformIndexHtml() {
buildSync({
minify: true,
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),
},
})
},
},
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The service worker is only built with esbuild during production builds (due to apply: 'build' on line 41), which means in development mode, the service worker file will be served with the literal string 'BUILD_VERSION' instead of an actual version. This will cause version mismatch errors when the main application (which will have the version replaced by Vite's define) tries to compare versions with the service worker.

Consider either:

  1. Using a Vite plugin that processes the service worker in both dev and build modes, or
  2. Configuring the dev server to properly handle the service worker transformation, or
  3. Documenting that dev mode won't work with service workers and developers should use build+preview mode for testing

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant