diff --git a/packages/core/src/compiler/scenePartsManifest.ts b/packages/core/src/compiler/scenePartsManifest.ts index 4e94bfd1b1f..b185aedcbf7 100644 --- a/packages/core/src/compiler/scenePartsManifest.ts +++ b/packages/core/src/compiler/scenePartsManifest.ts @@ -14,6 +14,7 @@ const hash = (text: string) => createHash("sha1").update(text).digest("hex").sli * `ignore` lists selectors for parts that change on every build without changing what is shown. */ export function addScenePartsManifest(html: string, ignore: readonly string[] = []): string { + if (!html.includes(SCENE_PART_ATTR)) return html; const doc = parseHTMLContent(html); const parts = [...doc.querySelectorAll(`[${SCENE_PART_ATTR}]`)]; if (parts.length === 0) return html; diff --git a/packages/studio-server/src/helpers/lazyPreviewImages.ts b/packages/studio-server/src/helpers/lazyPreviewImages.ts index 81202127eb8..bae09186663 100644 --- a/packages/studio-server/src/helpers/lazyPreviewImages.ts +++ b/packages/studio-server/src/helpers/lazyPreviewImages.ts @@ -15,9 +15,10 @@ const hasLoading = (img: Element) => export function lazyPreviewImages(html: string): string { if (!/]/i.test(html)) return html; - const images = [...parseHTML(html).document.querySelectorAll("img")]; // The scanner skips comments and raw text, so its `` element; the text " 0; } diff --git a/packages/studio-server/src/routes/preview.parseCount.test.ts b/packages/studio-server/src/routes/preview.parseCount.test.ts new file mode 100644 index 00000000000..086d3aaeff1 --- /dev/null +++ b/packages/studio-server/src/routes/preview.parseCount.test.ts @@ -0,0 +1,62 @@ +import { expect, it, vi } from "vitest"; +import { Hono } from "hono"; +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import type { StudioApiAdapter } from "../types"; + +const pageParses = vi.hoisted(() => ({ count: 0, minLength: Infinity })); +vi.mock("linkedom", async (actual) => { + const real = await actual(); + return { + ...real, + parseHTML: (html: string, ...rest: unknown[]) => { + if (html.length >= pageParses.minLength) pageParses.count += 1; + return (real.parseHTML as (...args: unknown[]) => ReturnType)( + html, + ...rest, + ); + }, + }; +}); + +const { registerPreviewRoutes } = await import("./preview"); + +it("parses a stamped page once per preview build (the read-tracking walk) when it has no images, or scene parts", async () => { + const dir = mkdtempSync(join(tmpdir(), "hf-preview-parses-")); + try { + const shapes = Array.from( + { length: 200 }, + (_, i) => ``, + ); + const page = + `t` + + `
${shapes.join("")}
` + + ``; + writeFileSync(join(dir, "index.html"), page); + const adapter = { + listProjects: () => [], + resolveProject: async (id: string) => ({ id, dir }), + bundle: async () => page, + lint: async () => ({ findings: [] }), + runtimeUrl: "/api/runtime.js", + rendersDir: () => "/tmp/renders", + startRender: () => ({ + id: "job", + status: "rendering", + progress: 0, + outputPath: "/tmp/out.mp4", + }), + } as unknown as StudioApiAdapter; + const app = new Hono(); + registerPreviewRoutes(app, adapter); + pageParses.minLength = page.length / 2; + + const response = await app.request("http://localhost/projects/demo/preview"); + + expect(response.status).toBe(200); + expect(pageParses.count).toBe(1); + } finally { + rmSync(dir, { recursive: true, force: true }); + } +});