UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

1 lines • 2.86 kB
{"version":3,"file":"version-check.mjs","names":[],"sources":["../../src/ui/version-check.ts"],"sourcesContent":["/**\n * The soft-refresh poller (atomic-deploys 10): a browser-side loop fetching\n * the host's `/.well-known/version` and reporting when the served deploy\n * fingerprint differs from the one this document was rendered from. No live\n * container swap — the banner decides whether to reload (old bytes are\n * retained, so a reload is always safe). Polling pauses while the document\n * is hidden and reports at most once.\n */\n\nconst DEFAULT_POLL_INTERVAL_MS = 60_000;\n\nexport interface VersionWatchHandle {\n stop: () => void;\n}\n\nexport function startVersionWatch(input: {\n currentFingerprint: string;\n onNewVersion: () => void;\n intervalMs?: number;\n endpoint?: string;\n fetchImpl?: typeof fetch;\n documentRef?: Document;\n}): VersionWatchHandle {\n const intervalMs = input.intervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const fetchImpl = input.fetchImpl ?? globalThis.fetch;\n const doc = input.documentRef ?? document;\n const endpoint = input.endpoint ?? \"/.well-known/version\";\n\n let reported = false;\n let timer: ReturnType<typeof setInterval> | null = null;\n\n const poll = async () => {\n if (reported) return;\n try {\n const response = await fetchImpl(endpoint, { headers: { accept: \"application/json\" } });\n if (!response.ok) return;\n const body = (await response.json()) as { fingerprint?: string };\n if (body.fingerprint && body.fingerprint !== input.currentFingerprint) {\n reported = true;\n input.onNewVersion();\n }\n } catch {\n // unreachable origin — the next tick retries\n }\n };\n\n const tick = () => {\n if (doc.visibilityState !== \"visible\") return;\n void poll();\n };\n\n timer = setInterval(tick, intervalMs);\n const onVisibility = () => tick();\n doc.addEventListener(\"visibilitychange\", onVisibility);\n\n return {\n stop: () => {\n if (timer) clearInterval(timer);\n timer = null;\n doc.removeEventListener(\"visibilitychange\", onVisibility);\n },\n };\n}\n"],"mappings":";;;;;;;;;AASA,MAAM,2BAA2B;AAMjC,SAAgB,kBAAkB,OAOX;CACrB,MAAM,aAAa,MAAM,cAAc;CACvC,MAAM,YAAY,MAAM,aAAa,WAAW;CAChD,MAAM,MAAM,MAAM,eAAe;CACjC,MAAM,WAAW,MAAM,YAAY;CAEnC,IAAI,WAAW;CACf,IAAI,QAA+C;CAEnD,MAAM,OAAO,YAAY;EACvB,IAAI,UAAU;EACd,IAAI;GACF,MAAM,WAAW,MAAM,UAAU,UAAU,EAAE,SAAS,EAAE,QAAQ,mBAAmB,EAAE,CAAC;GACtF,IAAI,CAAC,SAAS,IAAI;GAClB,MAAM,OAAQ,MAAM,SAAS,KAAK;GAClC,IAAI,KAAK,eAAe,KAAK,gBAAgB,MAAM,oBAAoB;IACrE,WAAW;IACX,MAAM,aAAa;GACrB;EACF,QAAQ,CAER;CACF;CAEA,MAAM,aAAa;EACjB,IAAI,IAAI,oBAAoB,WAAW;EACvC,AAAK,KAAK;CACZ;CAEA,QAAQ,YAAY,MAAM,UAAU;CACpC,MAAM,qBAAqB,KAAK;CAChC,IAAI,iBAAiB,oBAAoB,YAAY;CAErD,OAAO,EACL,YAAY;EACV,IAAI,OAAO,cAAc,KAAK;EAC9B,QAAQ;EACR,IAAI,oBAAoB,oBAAoB,YAAY;CAC1D,EACF;AACF"}