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.
This commit is contained in:
Shubham Padia
2025-09-17 07:46:50 +00:00
committed by Tim Abbott
parent 94f8198072
commit c3fdee6ed0

View File

@@ -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,
},
});
}
});