mirror of
https://github.com/fabro-sh/fabro.git
synced 2026-12-03 00:47:18 +00:00
A tab left open across a deploy keeps running the previous build's JavaScript indefinitely. index.html is fetched only on a full page load, all later navigation is client-side, and hashed bundles are served `immutable`, so nothing reveals that the code is stale. This produced a false-positive bug report where two correctly-deployed fixes appeared to be missing. Publishes a build id and offers a reload when the running document falls behind. The toast never reloads on its own; the only automatic reload is recovery from a chunk that no longer exists. Build id derivation ------------------- The obvious approach — hash the emitted asset filenames, which already embed content hashes — does not work: Bun's minified identifier naming is not deterministic. Building an unchanged tree twice produces byte-different output roughly one run in three (same length, ~100k differing bytes, all of it mangled names). Output hashes therefore move with no source change, which would fire the toast on redeploys of identical code and train people to ignore it. The id is instead derived from the bundle's source inputs, so it changes if and only if something we control changed. Verified stable across eight consecutive builds while the entry hash flipped between both variants. This non-determinism also means two builds of the same commit embed different bytes into the server binary, which is worth addressing separately for reproducible builds. Detection --------- SWR with `refreshInterval` + `revalidateOnFocus`, per the repo's React effects policy. SWR does not poll while the document is hidden, so background tabs stay quiet without extra gating. Unknown state on either side — missing meta tag, failed fetch, 503 during a dev rebuild — never produces a prompt. Stylesheet hashing ------------------ Tailwind's output was stable-named and therefore served `no-cache`, letting a tab revalidate into new CSS while running old JS. Tailwind purges unused classes per build, so classes the old bundle still emits could silently lose their styles. It is now content-hashed and moves with the build. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import { useEffect, useRef, useState, type RefObject } from "react";
|
|
|
|
import { importChunk } from "../../../lib/import-chunk";
|
|
|
|
/**
|
|
* Synchronizes a DOM container with a Graphviz-rendered SVG. Pipes the
|
|
* supplied DOT string through `@viz-js/viz` (the same layout engine Fabro
|
|
* uses on the server) and mounts the resulting `<svg>` into `containerRef`,
|
|
* stripping Graphviz's auto-inserted graph `<title>` so it doesn't surface
|
|
* as a browser tooltip. Re-renders whenever `dot` changes; re-applies the
|
|
* selection highlight on `selectedNodeId` change without paying the layout
|
|
* cost again.
|
|
*
|
|
* Returns the current `<svg>` element via a ref (handy for fit-to-window
|
|
* measurements) and an error string when rendering fails.
|
|
*/
|
|
export function useCanvasRender(
|
|
containerRef: RefObject<HTMLDivElement | null>,
|
|
dot: string,
|
|
selectedNodeId: string | null,
|
|
applyHighlight: (svg: SVGSVGElement, selectedId: string | null) => void,
|
|
): {
|
|
svgRef: RefObject<SVGSVGElement | null>;
|
|
error: string | null;
|
|
} {
|
|
const svgRef = useRef<SVGSVGElement | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
(async () => {
|
|
try {
|
|
const { instance } = await importChunk(() => import("@viz-js/viz"));
|
|
const viz = await instance();
|
|
if (cancelled) return;
|
|
const svg = viz.renderSVGElement(dot);
|
|
stripGraphTitle(svg);
|
|
svgRef.current = svg;
|
|
if (containerRef.current) {
|
|
containerRef.current.replaceChildren(svg);
|
|
}
|
|
applyHighlight(svg, selectedNodeId);
|
|
setError(null);
|
|
} catch (e) {
|
|
if (!cancelled) {
|
|
setError(e instanceof Error ? e.message : "Failed to render canvas");
|
|
}
|
|
}
|
|
})();
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [dot]);
|
|
|
|
useEffect(() => {
|
|
const svg = svgRef.current;
|
|
if (svg) applyHighlight(svg, selectedNodeId);
|
|
}, [selectedNodeId, applyHighlight]);
|
|
|
|
return { svgRef, error };
|
|
}
|
|
|
|
function stripGraphTitle(svg: SVGSVGElement) {
|
|
const title = svg.querySelector(".graph > title");
|
|
if (!title) return;
|
|
let sibling = title.nextElementSibling;
|
|
while (sibling && sibling.tagName === "text") {
|
|
const next = sibling.nextElementSibling;
|
|
sibling.remove();
|
|
sibling = next;
|
|
}
|
|
title.remove();
|
|
}
|