Skip to content

Write HTML. Render video. Built for agents.

fix(inspector): data-hf-id stamps alone do not make the live preview stale (#5870)

Xuanru Li8af8e48ad99f
diff --git a/packages/inspector/src/live.ts b/packages/inspector/src/live.ts
index 731cf27cf0..f36306d8f4 100644
--- a/packages/inspector/src/live.ts
+++ b/packages/inspector/src/live.ts
@@ -14,7 +14,12 @@ import {
} from "./engine";
import { liveGenerationPage, type PageRunner } from "./pageQueries";
import { assertGeneration, validateInspectRequest, validateTimelineRequest } from "./requests";
-import { Sources, authoredHtml, fingerprint, type AuthoredHtml } from "./sources";
+import {
+ Sources,
+ authoredHtml,
+ fingerprint as sourceFingerprint,
+ type AuthoredHtml,
+} from "./sources";
import type { InspectRequest, Snippet } from "./types";
export interface LiveInspection {
@@ -103,7 +108,9 @@ export function previewGeneration(runner: PageRunner): Promise<string | null> {
return runner.run(liveGenerationPage, undefined);
}
-export { fingerprint } from "./sources";
+export function fingerprint(project: string) {
+ return sourceFingerprint(project, { ignoreHfIds: true });
+}
export type {
ComponentReport,
ElementMapReport,
diff --git a/packages/inspector/src/sources.ts b/packages/inspector/src/sources.ts
index d5fbe729d4..ba728078e0 100644
--- a/packages/inspector/src/sources.ts
+++ b/packages/inspector/src/sources.ts
@@ -55,11 +55,16 @@ function sourceFiles(project: string) {
visit(project);
return files.sort();
}
-export function fingerprint(project: string) {
+const HF_ID_ATTRIBUTE = /\sdata-hf-id\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]+)/g;
+export function fingerprint(project: string, { ignoreHfIds = false } = {}) {
const hash = createHash("sha256");
for (const path of sourceFiles(project)) {
hash.update(relative(project, path));
- hash.update(readFileSync(path));
+ hash.update(
+ ignoreHfIds && extname(path) === ".html"
+ ? readFileSync(path, "utf8").replace(HF_ID_ATTRIBUTE, "")
+ : readFileSync(path),
+ );
}
return hash.digest("hex");
}
diff --git a/packages/inspector/tests/serviceFingerprint.test.ts b/packages/inspector/tests/serviceFingerprint.test.ts
new file mode 100644
index 0000000000..1ff77294ba
--- /dev/null
+++ b/packages/inspector/tests/serviceFingerprint.test.ts
@@ -0,0 +1,52 @@
+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { expect, it, vi } from "vitest";
+import { Inspector } from "../src/service";
+
+vi.mock("puppeteer-core", () => ({
+ default: {
+ launch: async () => ({
+ close: async () => {},
+ newPage: async () => {
+ let html = "";
+ return {
+ setViewport: async () => {},
+ goto: async (url: string) => {
+ html = await (await fetch(url)).text();
+ },
+ waitForFunction: async () => {},
+ evaluate: async () => {},
+ content: async () => html,
+ close: async () => {},
+ };
+ },
+ }),
+ },
+}));
+
+it("reloads the standalone page when an HF ID is stamped onto disk", async () => {
+ const project = mkdtempSync(join(tmpdir(), "inspect-stamp-"));
+ const path = join(project, "index.html");
+ const source =
+ '<!doctype html><html><head></head><body><main data-composition-id="root" data-duration="10" data-width="1920" data-height="1080"><div id="target">Title</div></main></body></html>';
+ writeFileSync(path, source);
+ const inspector = new Inspector(project, {
+ executablePath: "browser-stand-in",
+ instrument: false,
+ });
+ try {
+ const first = await inspector.withPage((page) => page.content());
+ expect(first).toContain('id="target"');
+ expect(first).not.toContain('data-hf-id="hf-t1"');
+ expect(await inspector.withPage((page) => page.content())).toBe(first);
+ expect(inspector.stats.pageLoads).toBe(1);
+ writeFileSync(path, source.replace('id="target"', 'id="target" data-hf-id="hf-t1"'));
+ const stamped = await inspector.withPage((page) => page.content());
+ expect(stamped).toContain('data-hf-id="hf-t1"');
+ expect(inspector.stats.pageLoads).toBe(2);
+ } finally {
+ await inspector.dispose();
+ rmSync(project, { recursive: true, force: true });
+ }
+});
diff --git a/packages/inspector/tests/sources.test.ts b/packages/inspector/tests/sources.test.ts
index 4f2f3d3da8..0a5f080cf8 100644
--- a/packages/inspector/tests/sources.test.ts
+++ b/packages/inspector/tests/sources.test.ts
@@ -3,6 +3,7 @@ import { mkdirSync, mkdtempSync, writeFileSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Sources, mergeSnippets, fingerprint } from "../src/sources";
+import { fingerprint as liveFingerprint } from "../src/live";
import { PREVIEW_IGNORED_DIRS } from "../../core/src/projectFolders";
import {
bundleToSingleHtml,
@@ -27,6 +28,25 @@ describe("source records", () => {
}
});
+ it("does not change when only data-hf-id stamps are added to the html", () => {
+ const dir = mkdtempSync(join(tmpdir(), "inspect-source-"));
+ try {
+ writeFileSync(join(dir, "index.html"), '<div class="a"><p>Hi</p></div>');
+ const before = liveFingerprint(dir);
+ const standaloneBefore = fingerprint(dir);
+ writeFileSync(
+ join(dir, "index.html"),
+ '<div class="a" data-hf-id="hf-1a2b"><p data-hf-id=\'hf-3c4d\'>Hi</p></div>',
+ );
+ expect(liveFingerprint(dir)).toBe(before);
+ expect(fingerprint(dir)).not.toBe(standaloneBefore);
+ writeFileSync(join(dir, "index.html"), '<div class="b" data-hf-id="hf-1a2b"><p>Hi</p></div>');
+ expect(liveFingerprint(dir)).not.toBe(before);
+ } finally {
+ rmSync(dir, { recursive: true, force: true });
+ }
+ });
+
it("keeps original multiline blocks and deduplicates targets without reformatting", () => {
const dir = mkdtempSync(join(tmpdir(), "inspect-source-"));
try {