From c3fdee6ed07c3971fae1599d73ec77d6da710d1e Mon Sep 17 00:00:00 2001 From: Shubham Padia Date: Wed, 17 Sep 2025 07:46:50 +0000 Subject: [PATCH] help: Add canonical URL pointing to zulip.com. Partially fixes #35110. We ideally want this only for self hosted installations since we want to point them to the right canonical URL. But we can have this for all pages since for zulip.com, this will be a self referencing URL. --- starlight_help/src/route_data.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/starlight_help/src/route_data.ts b/starlight_help/src/route_data.ts index 544109a187..0d59c62391 100644 --- a/starlight_help/src/route_data.ts +++ b/starlight_help/src/route_data.ts @@ -33,4 +33,25 @@ export const onRequest = defineRouteMiddleware((context) => { content: extractFirstParagraph(context.locals.starlightRoute.entry.body), }, }); + + const canonicalUrl = `https://zulip.com/help/${context.locals.starlightRoute.id}`; + const existingCanonicalTag = context.locals.starlightRoute.head.find( + (item) => item.tag === "link" && item.attrs?.rel === "canonical", + ); + + if (existingCanonicalTag) { + existingCanonicalTag.attrs!.href = canonicalUrl; + } else { + // Starlight already has the canonical tag by default and this might + // never get executed in practice. But it feels like a nice-to-have + // if any upstream changes happen in starlight and that behaviour + // changes. + context.locals.starlightRoute.head.push({ + tag: "link", + attrs: { + rel: "canonical", + href: canonicalUrl, + }, + }); + } });