Kez/kez-chat/web/vite.config.ts
Jason Tudisco d789e872b1 feat(kez-chat/web): SW auto-reload on deploy + visible build sha in footer
Two related changes — both aimed at "can you tell what's deployed?"

1. SW auto-update (no more "refresh twice")

   The default vite-plugin-pwa autoUpdate behavior was: new SW
   downloads on first reload, activates on second reload. Users
   refresh after a deploy, still see old bundle, get confused.

   Now:
   • workbox: skipWaiting + clientsClaim → new SW activates and
     takes control of existing pages immediately on install.
   • main.ts listens for `controllerchange` and calls reload() once.
     New SW takes over → page reloads → new bundle loads.

   Net: deploys land on the FIRST refresh after the new bundle is
   reachable. (Caveat: the SW that's currently running has to
   download the new SW first, so the very first refresh after a
   deploy may serve stale + then auto-reload a beat later.)

2. Visible build sha in the footer

   vite.config.ts now runs `git rev-parse --short HEAD` at build
   time and injects __BUILD_SHA__ + __BUILD_TIME__ via Vite's
   `define`. App.svelte's footer renders the sha as a small monospace
   chip linking to the commit on gitea, with the build time on
   hover.

   "kez-chat web v0.1" → "kez-chat  [abc1234]  · source"

   So when you refresh and the chip changes value, you know the new
   build landed. When it doesn't, you know the SW is still serving
   the old bundle.

3. Killed the `apple-mobile-web-app-capable` deprecation warning by
   adding the standard `mobile-web-app-capable` next to it.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-26 23:58:26 -06:00

101 lines
3.4 KiB
TypeScript

import { execSync } from "node:child_process";
import { defineConfig } from "vite";
import { svelte } from "@sveltejs/vite-plugin-svelte";
import tailwindcss from "@tailwindcss/vite";
import { VitePWA } from "vite-plugin-pwa";
// Build-time identity: short git SHA + ISO date. Surfaced in the footer
// as "kez-chat <sha>" so users can eyeball which build they're on (handy
// when verifying that a deploy actually landed instead of being cached).
const BUILD_SHA = (() => {
try {
return execSync("git rev-parse --short HEAD").toString().trim();
} catch {
return "dev";
}
})();
const BUILD_TIME = new Date().toISOString();
export default defineConfig({
define: {
__BUILD_SHA__: JSON.stringify(BUILD_SHA),
__BUILD_TIME__: JSON.stringify(BUILD_TIME),
},
plugins: [
svelte(),
tailwindcss(),
VitePWA({
// Auto-update: a new SW activates on next page load. No "click to
// update" prompt — chat needs to stay fresh and we don't want users
// stuck on an old build.
registerType: "autoUpdate",
injectRegister: "auto",
manifest: {
name: "kez-chat",
short_name: "kez-chat",
description:
"End-to-end encrypted chat on top of KEZ — portable cross-app identity.",
start_url: "/",
scope: "/",
display: "standalone",
background_color: "#111827",
theme_color: "#111827",
categories: ["social", "communication"],
icons: [
{ src: "pwa-64x64.png", sizes: "64x64", type: "image/png" },
{ src: "pwa-192x192.png", sizes: "192x192", type: "image/png" },
{ src: "pwa-512x512.png", sizes: "512x512", type: "image/png" },
{
src: "maskable-icon-512x512.png",
sizes: "512x512",
type: "image/png",
purpose: "maskable",
},
],
},
workbox: {
// Activate new SW immediately + take control of existing pages
// without waiting for them to close. Paired with the
// controllerchange reload in main.ts, this means deploys land
// on the first refresh instead of the second.
skipWaiting: true,
clientsClaim: true,
// Precache the SPA shell. Chat data is fetched live from /v1/*
// and we DON'T want it cached — see runtimeCaching below.
globPatterns: ["**/*.{js,css,html,svg,png,ico,wasm}"],
// zstd wasm is ~350 KB; raise the per-file cap.
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024,
// Same-origin navigation requests fall back to the SPA shell so
// /messages, /claims, etc. work after a refresh while offline.
navigateFallback: "index.html",
navigateFallbackDenylist: [/^\/v1\//, /^\/internal\//, /^\/\.well-known\//],
runtimeCaching: [
{
// Never cache API responses — they're authenticated + dynamic.
urlPattern: /\/v1\//,
handler: "NetworkOnly",
},
],
navigationPreload: true,
cleanupOutdatedCaches: true,
},
devOptions: {
enabled: false,
},
}),
],
server: {
// For dev: proxy API calls to the locally-running chat-server.
// The deployed SPA (served by the same chat-server) doesn't need this.
proxy: {
"/v1": "http://localhost:6969",
"/internal": "http://localhost:6969",
"/.well-known": "http://localhost:6969",
},
},
build: {
outDir: "dist",
emptyOutDir: true,
},
});