Files
webspeak3/web/vite.config.ts
T
MoepchiandClaude Opus 5 2ee6d1f0b5 Stop the service worker from stranding clients
Between 48f5e35 (07.09., registerType: 'autoUpdate') and 5b5f4ca (10.09.,
switched to 'prompt') every client that cached the app froze: the new
worker sits in `waiting` and the only code that could release it - the
update toast - lives in the build behind that worker. Their next update
never arrives, and Pages deletes the hashed assets they still run from,
so the bundle they serve does not exist on the server any more. That is
how the feedback form went missing on client.webspeak3.de.

- Back to 'autoUpdate', with skipWaiting/clientsClaim spelled out: the
  plugin only sets them when it also injects registerSW.js, so with
  injectRegister: false the generated worker was byte-identical to the
  'prompt' one. Verified in dist/sw.js.
- onNeedReload instead of onNeedRefresh: the worker takes over on its
  own, the toast only decides *when* to reload, so nobody is thrown out
  of a running connection.
- version.json plus a __BUILD_ID__ baked into the bundle, checked every
  30 min and on tab focus, deliberately routed around the worker (not in
  globPatterns, so never precached). If the server is on another build
  and no new worker turns up, the worker is wedged and the button clears
  registrations and caches instead of reloading into the same cache.
- Dismissing the toast snoozes for 30 min rather than for good.

Verified in headless Chromium against simulated deploys: a client stuck
on the old worker lands on the new bundle at the first plain F5.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
2026-09-11 12:20:53 +00:00

105 lines
4.4 KiB
TypeScript

import { execSync } from 'node:child_process'
import { defineConfig, type Plugin } from 'vite'
import react from '@vitejs/plugin-react'
import { VitePWA } from 'vite-plugin-pwa'
// Identifies the build a bundle came from. Only has to differ between two
// builds — the client compares its own baked-in value against version.json to
// notice when it is running something the server no longer serves.
function resolveBuildId(): string {
const fromCi = process.env.CF_PAGES_COMMIT_SHA || process.env.GITHUB_SHA
if (fromCi) return fromCi.slice(0, 12)
try {
return execSync('git rev-parse --short=12 HEAD', { stdio: ['ignore', 'pipe', 'ignore'] })
.toString()
.trim()
} catch {
// No git checkout (the Docker build copies sources in without .git).
return String(Date.now())
}
}
const buildId = resolveBuildId()
// version.json must never be precached, or a stuck client would ask the very
// cache it is stuck in what the server has. `.json` is not in globPatterns
// below, so Workbox leaves it alone and the fetch always hits the network.
function versionManifest(): Plugin {
return {
name: 'webspeak3-version-manifest',
apply: 'build',
generateBundle() {
this.emitFile({
type: 'asset',
fileName: 'version.json',
source: `${JSON.stringify({ buildId })}\n`,
})
},
}
}
// https://vite.dev/config/
export default defineConfig({
define: {
__BUILD_ID__: JSON.stringify(buildId),
},
plugins: [
react(),
versionManifest(),
VitePWA({
// The generated worker takes over as soon as it has installed
// (skipWaiting + clientsClaim). It must NOT wait for the page to ask it
// to: between 2026-09-07 and 2026-09-10 this was `prompt`, and every
// client cached in that window froze — the new worker sat in `waiting`
// forever because the only code that could have released it (the update
// toast) lived in the version behind that worker. Deciding *when* to
// reload is a UI concern and stays one: `onNeedReload` in App.tsx shows
// the toast instead of pulling the page out from under an active
// connection.
registerType: 'autoUpdate',
// We register from inside the app (virtual:pwa-register/react) rather
// than through an injected registerSW.js, because that script would
// reload the page on its own the moment a new worker activates.
injectRegister: false,
// Dev-time Workbox would sit in front of Vite's own HMR/WS traffic and
// stale-cache the app shell while iterating — only ever ship the SW in
// production builds.
devOptions: { enabled: false },
includeAssets: ['favicon.ico', 'apple-touch-icon.png'],
manifest: {
name: 'WebSpeak3',
short_name: 'WebSpeak3',
description: 'An independent, self-hosted web client for TeamSpeak 3 servers.',
theme_color: '#03060d',
background_color: '#03060d',
display: 'standalone',
start_url: '.',
icons: [
{ src: 'favicon-192.png', sizes: '192x192', type: 'image/png' },
{ src: 'icon-512.png', sizes: '512x512', type: 'image/png' },
{ src: 'icon-512-maskable.png', sizes: '512x512', type: 'image/png', purpose: 'maskable' },
],
},
workbox: {
// Both are what `registerType: 'autoUpdate'` is supposed to imply, but
// the plugin only sets them when it also injects registerSW.js (see
// resolveOptions: `injectRegister === 'auto' || injectRegister == null`)
// — with injectRegister: false they have to be spelled out, or the
// generated worker is byte-identical to the `prompt` one and strands
// clients all over again. Verified in dist/sw.js after building.
skipWaiting: true,
clientsClaim: true,
// Precache the built app shell (JS/CSS/HTML/icons) only. The gateway's
// /ws endpoint and any file-transfer/API traffic must never be
// intercepted or cached — Workbox's default fetch handler already
// ignores non-GET and non-http(s) requests (ws:// isn't touched), so
// no extra runtimeCaching exclusions are needed here.
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2}'],
},
}),
],
// Set by the GitHub Pages workflow to the repo subpath (e.g. /webspeak3/);
// the normal Docker build serves from the domain root and leaves this unset.
base: process.env.VITE_BASE_PATH || '/',
})