diff --git a/apps/storefront/scripts/seo-metadata.test.mjs b/apps/storefront/scripts/seo-metadata.test.mjs index de2327c..79a42d1 100644 --- a/apps/storefront/scripts/seo-metadata.test.mjs +++ b/apps/storefront/scripts/seo-metadata.test.mjs @@ -1,6 +1,7 @@ import assert from "node:assert/strict"; import { readFile } from "node:fs/promises"; import test from "node:test"; +import { JSDOM } from "jsdom"; const appRoot = new URL("../", import.meta.url); @@ -42,7 +43,7 @@ test("prerender emits complete CMS metadata in the first document response", asy test("client SEO removes generated metadata without restoring stale tags", async () => { const seo = await readFile(new URL("../../packages/ui/src/seo/Seo.tsx", appRoot), "utf8"); - assert.match(seo, /querySelectorAll\([\s\S]*data-storefront-seo/); + assert.match(seo, /querySelectorAll\([\s\S]*data-storefront-seo\]:not\(\[data-rh\]\)/); assert.doesNotMatch(seo, /insertBefore\(element/); assert.match(seo, /image\?\.url \|\| opengraphImage/); assert.match(seo, /property="og:image:alt"/); @@ -50,6 +51,22 @@ test("client SEO removes generated metadata without restoring stale tags", async assert.match(seo, /name="twitter:image:alt"/); }); +test("client SEO cleanup preserves prerendered titles adopted by Helmet", () => { + const dom = new JSDOM(` + Adopted title + + `); + + dom.window.document.head + .querySelectorAll( + '[data-storefront-seo]:not([data-rh]), meta[name="description"]:not([data-rh]), link[rel="canonical"]:not([data-rh])', + ) + .forEach((element) => element.remove()); + + assert.equal(dom.window.document.title, "Adopted title"); + assert.equal(dom.window.document.querySelector('meta[name="description"]'), null); +}); + test("controlled crawler files remain exact and product feed uses its canonical address", async () => { const prerender = await readFile(new URL("scripts/prerender.mjs", appRoot), "utf8"); const feedDiscovery = await readFile(new URL("src/components/GlobalFeedDiscovery.tsx", appRoot), "utf8"); diff --git a/packages/ui/src/seo/Seo.tsx b/packages/ui/src/seo/Seo.tsx index f4b20d9..35dd984 100644 --- a/packages/ui/src/seo/Seo.tsx +++ b/packages/ui/src/seo/Seo.tsx @@ -121,7 +121,7 @@ export function Seo({ useLayoutEffect(() => { document.head .querySelectorAll( - '[data-storefront-seo], meta[name="description"]:not([data-rh]), link[rel="canonical"]:not([data-rh])', + '[data-storefront-seo]:not([data-rh]), meta[name="description"]:not([data-rh]), link[rel="canonical"]:not([data-rh])', ) .forEach((element) => element.remove()); }, []);