Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix(branding): run the in-page branding scan once per page (#4884)

Abimael Martell45226206f62e
diff --git a/apps/api/src/__tests__/lib/branding/branding-script-runs-once.test.ts b/apps/api/src/__tests__/lib/branding/branding-script-runs-once.test.ts
new file mode 100644
index 000000000..642327ece
--- /dev/null
+++ b/apps/api/src/__tests__/lib/branding/branding-script-runs-once.test.ts
@@ -0,0 +1,32 @@
+import { describe, it, expect } from "vitest";
+import { JSDOM } from "jsdom";
+
+import { getBrandingScript } from "../../../scraper/scrapeURL/engines/fire-engine/brandingScript";
+
+describe("bundled branding script", () => {
+ it("scans the page once and returns the result", () => {
+ const dom = new JSDOM(
+ `<html><head><title>Acme | Home</title><script src="/app.js"></script></head>
+ <body><header><a href="/"><img alt="Acme logo" src="/logo.svg"></a></header>
+ <h1>Acme</h1><p>Hello</p><button>Get started</button></body></html>`,
+ { runScripts: "outside-only", url: "https://acme.test/" },
+ );
+ const window = dom.window as any;
+
+ // Framework detection reads the page's script tags once per scan.
+ let scans = 0;
+ const querySelectorAll = window.Document.prototype.querySelectorAll;
+ window.Document.prototype.querySelectorAll = function (selector: string) {
+ if (selector === "script[src]") scans++;
+ return querySelectorAll.call(this, selector);
+ };
+
+ const result = window.eval(getBrandingScript());
+
+ expect(scans).toBe(1);
+ // A real extraction, not just an object: the brand comes from the title,
+ // and the scan recorded no errors.
+ expect(result?.branding?.brandName).toBe("Acme");
+ expect(result?.branding?.errors).toBeUndefined();
+ });
+});
diff --git a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/index.ts b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/index.ts
index 8a59cc9bd..870bee47a 100644
--- a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/index.ts
+++ b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/index.ts
@@ -65,8 +65,3 @@ export const extractBrandDesign = (): BrandingResult => {
},
};
};
-
-// Auto-execute when loaded in browser context (IIFE pattern)
-(function __extractBrandDesign() {
- return extractBrandDesign();
-})();