Skip to content

Commit a412c13

Browse files
aka-sacci-ccrdecobotclaude
authored
fix(blog): strip query string and hash from SEO canonical and JSON-LD URLs (#1645)
Requests arriving with ?utm_source=..., ?page=2 or a #hash were leaking those into the canonical link, the BlogPosting/Blog url and mainEntityOfPage, and the BreadcrumbList items emitted by SeoBlogPost and SeoBlogPostListing. Normalize in withCanonicalBase so every emitted URL is origin + pathname only, both with and without a configured canonicalBaseUrl. Relative URLs without a base still pass through unchanged. Co-authored-by: decobot <capy@deco.cx> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent aae0ee6 commit a412c13

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

blog/utils/jsonLD.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@ export const toOrganization = (publisher: Publisher) => ({
2525
});
2626

2727
/**
28-
* Replaces the origin of a URL, keeping path, query and hash. Relative URLs
29-
* are resolved against the canonical base.
28+
* Normalizes a page URL for SEO use (canonical and JSON-LD): strips the query
29+
* string and hash, keeping only origin + pathname, and replaces the origin
30+
* with the canonical base's when one is configured. Relative URLs are
31+
* resolved against the canonical base; without a base, relative URLs are
32+
* returned unchanged.
3033
*/
3134
export const withCanonicalBase = (url: string, canonicalBaseUrl?: string) => {
3235
if (!canonicalBaseUrl) {
33-
return url;
36+
if (!URL.canParse(url)) {
37+
return url;
38+
}
39+
const { origin, pathname } = new URL(url);
40+
return origin + pathname;
3441
}
35-
const { pathname, search, hash } = new URL(url, canonicalBaseUrl);
36-
return new URL(pathname + search + hash, canonicalBaseUrl).href;
42+
const { pathname } = new URL(url, canonicalBaseUrl);
43+
return new URL(pathname, canonicalBaseUrl).href;
3744
};
3845

3946
/**

0 commit comments

Comments
 (0)