From 2b56c451778a5af04c480b6e7b02fbea98376308 Mon Sep 17 00:00:00 2001 From: David Cumps Date: Wed, 21 Oct 2026 22:09:07 +0000 Subject: [PATCH] fix(seo): rebase only the exact default authority, insert SITE_URL literally Review follow-up: the origin regex also matched a port (https://www.bentopdf.com:8443) and userinfo (https://www.bentopdf.com@other.example), which are different origins; the lookahead now excludes ':' and '@'. The replacement is a callback, so `$&`/`$1` in SITE_URL stay literal. Tests cover both. --- scripts/site-origin.mjs | 12 ++++++++---- src/tests/site-origin.test.ts | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/scripts/site-origin.mjs b/scripts/site-origin.mjs index 920bdef8..ae5e9a5b 100644 --- a/scripts/site-origin.mjs +++ b/scripts/site-origin.mjs @@ -4,16 +4,20 @@ */ export const DEFAULT_SITE_URL = 'https://www.bentopdf.com'; -const DEFAULT_ORIGIN_RE = /https:\/\/www\.bentopdf\.com(?![\w.-])/g; +// The exact authority only: not a longer host (`.com.example`), a port +// (`:8443`, a different origin) or userinfo (`@other.example`, another host). +const DEFAULT_ORIGIN_RE = /https:\/\/www\.bentopdf\.com(?![\w.:@-])/g; /** * Rewrites absolute URLs on the default origin to `siteUrl` (no trailing * slash). Used for hand-written pages that carry the official origin * literally, so a build with a custom SITE_URL emits canonicals on its own - * host. Other hosts, including look-alikes such as - * `https://www.bentopdf.com.example`, are left untouched. + * host. Other origins are left untouched: look-alike hosts such as + * `https://www.bentopdf.com.example`, other ports and userinfo URLs. */ export function rebaseDefaultOrigin(html, siteUrl) { if (siteUrl === DEFAULT_SITE_URL) return html; - return html.replace(DEFAULT_ORIGIN_RE, siteUrl); + // A callback keeps `siteUrl` literal: a string replacement would expand + // `$&`, `$1`, … inside it. + return html.replace(DEFAULT_ORIGIN_RE, () => siteUrl); } diff --git a/src/tests/site-origin.test.ts b/src/tests/site-origin.test.ts index 3ff994f4..16a7e4e7 100644 --- a/src/tests/site-origin.test.ts +++ b/src/tests/site-origin.test.ts @@ -35,6 +35,21 @@ describe('rebaseDefaultOrigin', () => { expect(rebaseDefaultOrigin(html, 'https://pdf.example.org')).toBe(html); }); + it('leaves other ports and userinfo URLs alone', () => { + const html = + 'href="https://www.bentopdf.com:8443/blog/" href="https://www.bentopdf.com@other.example/"'; + expect(rebaseDefaultOrigin(html, 'https://pdf.example.org')).toBe(html); + }); + + it('inserts the site URL literally', () => { + expect( + rebaseDefaultOrigin( + 'href="https://www.bentopdf.com/blog/"', + 'https://example.org/$&' + ) + ).toBe('href="https://example.org/$&/blog/"'); + }); + it('is a no-op for the default site URL', () => { expect(rebaseDefaultOrigin(PAGE, DEFAULT_SITE_URL)).toBe(PAGE); });