Change hardcoded SW_VERSION to use dynamic version at build time#10
Change hardcoded SW_VERSION to use dynamic version at build time#10
Conversation
Don't edit files that get committed to git.
There was a problem hiding this comment.
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
defineto 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.
| { | ||
| 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), | ||
| }, | ||
| }) | ||
| }, | ||
| }, |
There was a problem hiding this comment.
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:
- Using a Vite plugin that processes the service worker in both dev and build modes, or
- Configuring the dev server to properly handle the service worker transformation, or
- Documenting that dev mode won't work with service workers and developers should use build+preview mode for testing
Don't edit files that get committed to git.