Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions blog/utils/jsonLD.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,22 @@ export const toOrganization = (publisher: Publisher) => ({
});

/**
* Replaces the origin of a URL, keeping path, query and hash. Relative URLs
* are resolved against the canonical base.
* Normalizes a page URL for SEO use (canonical and JSON-LD): strips the query
* string and hash, keeping only origin + pathname, and replaces the origin
* with the canonical base's when one is configured. Relative URLs are
* resolved against the canonical base; without a base, relative URLs are
* returned unchanged.
*/
export const withCanonicalBase = (url: string, canonicalBaseUrl?: string) => {
if (!canonicalBaseUrl) {
return url;
if (!URL.canParse(url)) {
return url;
}
const { origin, pathname } = new URL(url);
return origin + pathname;
}
const { pathname, search, hash } = new URL(url, canonicalBaseUrl);
return new URL(pathname + search + hash, canonicalBaseUrl).href;
const { pathname } = new URL(url, canonicalBaseUrl);
return new URL(pathname, canonicalBaseUrl).href;
};

/**
Expand Down
Loading