diff --git a/site/package.json b/site/package.json index 843d4bae..3769b038 100644 --- a/site/package.json +++ b/site/package.json @@ -9,7 +9,7 @@ "lint": "next lint", "typecheck": "tsc --noEmit", "fetch:rates": "tsx scripts/fetch-rates.ts", - "prebuild": "node --test scripts/workdays-faq-parity.test.mjs && node scripts/check-site-url.mjs && tsx scripts/fetch-rates.ts && node scripts/check-pressure-reference.mjs && node scripts/check-currency-rate-freshness.mjs", + "prebuild": "node --test scripts/workdays-faq-parity.test.mjs && node --test scripts/sharebar-state-navigation.test.mjs && node scripts/check-site-url.mjs && tsx scripts/fetch-rates.ts && node scripts/check-pressure-reference.mjs && node scripts/check-currency-rate-freshness.mjs", "sitemap": "tsx scripts/generate-sitemap.ts", "validate": "tsx scripts/validate.ts", "audit:seo": "tsx scripts/audit-seo.ts", diff --git a/site/scripts/sharebar-state-navigation.test.mjs b/site/scripts/sharebar-state-navigation.test.mjs new file mode 100644 index 00000000..c7cd9590 --- /dev/null +++ b/site/scripts/sharebar-state-navigation.test.mjs @@ -0,0 +1,20 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; + +const share = readFileSync(new URL("../src/components/tools/ShareBar.tsx", import.meta.url), "utf8"); + +test("share state resynchronizes after URL or tool slug changes", () => { + assert.match(share, /setHasResultLink\(new URLSearchParams\(window\.location\.search\)\.has\("s"\)\)/); + assert.match(share, /\}, \[url, slug\]\);/); + for (const event of ["toolify:share-state-change", "popstate", "pageshow"]) { + assert.ok(share.includes(`window.addEventListener("${event}", sync)`), event + " listener"); + assert.ok(share.includes(`window.removeEventListener("${event}", sync)`), event + " cleanup"); + } +}); + +test("safe embed and tracked share attribution stay intact", () => { + assert.match(share, /buildToolEmbedSnippet\(title, embedUrl, url\)/); + assert.match(share, /socialShareUrl\(url, source\)/); + assert.match(share, /if \(isShareCancelled\(error\)\) return;/); +}); diff --git a/site/src/components/tools/ShareBar.tsx b/site/src/components/tools/ShareBar.tsx index bde0a4ca..2fa5486b 100644 --- a/site/src/components/tools/ShareBar.tsx +++ b/site/src/components/tools/ShareBar.tsx @@ -118,7 +118,7 @@ export function ShareBar({ url, embedUrl, title, slug }: Props) { window.removeEventListener("popstate", sync); window.removeEventListener("pageshow", sync); }; - }, []); + }, [url, slug]); // Recheck shared-result state on locale/tool navigation. const enc = encodeURIComponent; // Keep optional embed attribution visible to readers without ranking-credit links.