From 1e74c69228ea2991f9eba35440b4f68b2f5f1c20 Mon Sep 17 00:00:00 2001 From: Simon Date: Wed, 16 Sep 2026 23:00:45 +0200 Subject: [PATCH] fix(capture): run page.evaluate as plain string (tsx/esbuild __name helpers break in browser context) --- apps/api/src/capture/capture.ts | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/apps/api/src/capture/capture.ts b/apps/api/src/capture/capture.ts index 52aed65..18655e0 100644 --- a/apps/api/src/capture/capture.ts +++ b/apps/api/src/capture/capture.ts @@ -138,9 +138,11 @@ export async function capturePage( }); const response = await page.goto(url, { timeout: opts.timeoutMs, waitUntil: "domcontentloaded" }); await page.waitForTimeout(500); - const captured = await page.evaluate(() => { - const count = (sel: string) => document.querySelectorAll(sel).length; - const pick = (el: Element | null, props: string[]) => { + // NOTE: evaluate code must be a plain string — tsx/esbuild would inject + // __name helpers into a transpiled function, which break in the browser. + const captured = await page.evaluate(`(() => { + const count = (sel) => document.querySelectorAll(sel).length; + const pick = (el, props) => { if (!el) return Object.fromEntries(props.map((p) => [p, null])); const cs = getComputedStyle(el); return Object.fromEntries(props.map((p) => [p, cs.getPropertyValue(p) || null])); @@ -176,13 +178,21 @@ export async function capturePage( video: count("video"), canvas: count("canvas"), }, - stylesheets: Array.from(document.querySelectorAll('link[rel="stylesheet"]')).map((l) => - (l as HTMLLinkElement).href, - ), + stylesheets: Array.from(document.querySelectorAll('link[rel="stylesheet"]')).map((l) => l.href), images: Array.from(document.querySelectorAll("img")).slice(0, 50).map((i) => i.src), computedStyle: { body, h1 }, }; - }); + })()`) as { + title: string; + description: string | null; + lang: string | null; + charset: string; + viewportMeta: string | null; + domCounts: Record; + stylesheets: string[]; + images: string[]; + computedStyle: Record>; + }; let screenshotBase64: string | null = null; try { screenshotBase64 = await page.screenshot({