When a new version is deployed, open tabs no longer silently run stale code until a manual F5. - Server stamps every response with X-App-Version (src/middleware/version.ts, onSend hook in server.ts). - Client bakes its build version via Vite define (__APP_VERSION__); apiFetch compares the header to it (no polling — piggybacks existing traffic) and latches a mismatch in a small store (appVersion.ts). - UpdateBanner: a persistent bottom Snackbar "Je k dispozici nová verze aplikace. — Obnovit" → location.reload(). User-initiated, so an in-progress form is never interrupted. - lazyWithReload: every React.lazy page reloads once if its hashed chunk 404s after a deploy (sessionStorage-guarded against loops), fixing the "old chunk gone" error screen. Approach chosen after researching current SPA practice (version header + banner beats polling / a service worker for a non-PWA admin app). Tests: +4 (server header, client store latch/notify). Full suite 669 pass, tsc -b clean, lint 0. Verified live via Playwright (header emitted; banner appears on a simulated mismatch). Detection applies to deploys from the NEXT release onward (current clients lack the detection code). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
25 lines
839 B
TypeScript
25 lines
839 B
TypeScript
import { describe, it, expect } from "vitest";
|
|
import Fastify from "fastify";
|
|
import { registerVersionHeader, readAppVersion } from "../middleware/version";
|
|
|
|
/**
|
|
* The server stamps every response with X-App-Version so the SPA can detect a
|
|
* deploy (the client compares it to the version baked into its bundle).
|
|
*/
|
|
|
|
describe("registerVersionHeader", () => {
|
|
it("adds X-App-Version to responses", async () => {
|
|
const app = Fastify({ logger: false });
|
|
registerVersionHeader(app, "9.9.9");
|
|
app.get("/ping", async () => ({ ok: true }));
|
|
|
|
const res = await app.inject({ method: "GET", url: "/ping" });
|
|
expect(res.headers["x-app-version"]).toBe("9.9.9");
|
|
await app.close();
|
|
});
|
|
|
|
it("defaults to the package.json version (semver string)", () => {
|
|
expect(readAppVersion()).toMatch(/^\d+\.\d+\.\d+/);
|
|
});
|
|
});
|