1import { CLIENT_MARKER } from "./client-marker.ts";
2import type * as ClientMarker from "./client-marker.ts";
3
4const marker = document.querySelector<HTMLParagraphElement>("#client-marker")!;
5const render = (value: string) => {
6 marker.textContent = value;
7};
8render(CLIENT_MARKER);
9
10// The counter is the HMR state witness: a HOT update to ./client-marker.ts
11// leaves this module (and `count`) alive, while a full page reload resets
12// the counter to 0 — the client HMR spec asserts the count survives.
13const button = document.querySelector<HTMLButtonElement>("#counter")!;
14let count = 0;
15const setCount = (next: number) => {
16 count = next;
17 button.textContent = `Count is ${count}`;
18};
19button.addEventListener("click", () => setCount(count + 1));
20setCount(0);
21
22if (import.meta.hot) {
23 // Accept updates to the marker module only: vite re-fetches the dep and
24 // runs this callback in place — this module is NOT re-executed.
25 import.meta.hot.accept("./client-marker.ts", (mod) => {
26 if (mod) {
27 render((mod as typeof ClientMarker).CLIENT_MARKER);
28 }
29 });
30}