From f6ee49362938b956c9a3129cbd36de89399e6edb Mon Sep 17 00:00:00 2001 From: Jan Svabenik Date: Tue, 26 May 2026 11:56:09 +0200 Subject: [PATCH] fix: bust PWA cache on each release build sw.js uses __BUILDVER__ token; build.ps1 replaces it with the git version before go build (which embeds the patched file), then restores the template via the finally block. Installed PWA clients receive new assets after every release. Co-Authored-By: Claude Sonnet 4.6 --- build.ps1 | 34 +++++++++++++++++++++++++--------- web/static/sw.js | 7 +++++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/build.ps1 b/build.ps1 index 738d357..001b537 100644 --- a/build.ps1 +++ b/build.ps1 @@ -31,17 +31,33 @@ try { $out = Join-Path $OutDir "roadamp.exe" - Write-Host "Building roadamp $Version → $out" -ForegroundColor Cyan + Write-Host "Building roadamp $Version -> $out" -ForegroundColor Cyan - $env:GOOS = "windows" - $env:GOARCH = "amd64" - $env:CGO_ENABLED = "0" + # ── Patch sw.js with build version ──────────────────────────────────────── + # The service worker embeds a cache-bust token so installed PWA clients + # pick up new assets after each release. We replace __BUILDVER__ in the + # source file, run go build (which embeds it), then restore via git. + $swPath = Join-Path $PSScriptRoot 'web\static\sw.js' + $swOriginal = Get-Content $swPath -Raw + try { + $swPatched = $swOriginal -replace '__BUILDVER__', $Version + [System.IO.File]::WriteAllText($swPath, $swPatched, [System.Text.Encoding]::UTF8) - go build ` - -trimpath ` - -ldflags "-s -w -X main.Version=$Version" ` - -o $out ` - ./cmd/roadamp + # ── Compile ─────────────────────────────────────────────────────────── + $env:GOOS = "windows" + $env:GOARCH = "amd64" + $env:CGO_ENABLED = "0" + + go build ` + -trimpath ` + -ldflags "-s -w -X main.Version=$Version" ` + -o $out ` + ./cmd/roadamp + } + finally { + # Always restore sw.js so the template token is preserved in the repo. + [System.IO.File]::WriteAllText($swPath, $swOriginal, [System.Text.Encoding]::UTF8) + } $size = [math]::Round((Get-Item $out).Length / 1MB, 2) Write-Host "Done $out (${size} MB)" -ForegroundColor Green diff --git a/web/static/sw.js b/web/static/sw.js index 24e2d0d..5bf6aa8 100644 --- a/web/static/sw.js +++ b/web/static/sw.js @@ -1,6 +1,9 @@ -'use strict'; +'use strict'; -const CACHE = 'roadamp-v1'; +// Cache name is injected by build.ps1 (token replaced with git version). +// When the binary is built without the script, __BUILDVER__ stays literal +// and the SW still works — it just won't auto-bust on updates. +const CACHE = 'roadamp-__BUILDVER__'; const SHELL = ['/', '/app.js', '/style.css', '/manifest.json', '/icon.svg']; self.addEventListener('install', e => {