Skip to content

Commit 8f11c76

Browse files
IEvangelistCopilot
andauthored
Strip locale prefix from API reference sidebar links (#755)
API reference pages under /reference/api/ are only generated for the default (English) locale because they're produced from package metadata rather than translated content. However, Starlight's i18n auto-prefixes the active locale to sidebar links (e.g. /it/reference/api/csharp/), which produces 404s for any non-default locale. Rather than redirecting at request time, fix the source: extend the existing route-data middleware to walk the sidebar tree and rewrite hrefs that match the localized API reference pattern back to their canonical, locale-stripped path. The sidebar then never emits a link that 404s, regardless of the active locale. Reuses stripApiReferenceLocale from src/utils/api-reference-routes (added in #754) so locale detection stays in one place. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 252e547 commit 8f11c76

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

src/frontend/src/route-data-middleware.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineRouteMiddleware } from '@astrojs/starlight/route-data';
2+
import { stripApiReferenceLocale } from './utils/api-reference-routes';
23

34
/**
45
* Custom route middleware that applies implicit pagination rules:
@@ -22,6 +23,15 @@ export const onRequest = defineRouteMiddleware((context) => {
2223

2324
const { entry, pagination, sidebar } = routeData;
2425

26+
// --- Step 0: Canonicalize API reference sidebar links ---
27+
// API reference pages under /reference/api/ are only generated for the default
28+
// (English) locale because they're produced from package metadata rather than
29+
// translated content. Starlight auto-prefixes the active locale to sidebar
30+
// links, which produces 404s for non-default locales (e.g.
31+
// /it/reference/api/csharp/). Rewrite those hrefs to point to the canonical
32+
// English path so the sidebar never emits a link that 404s.
33+
canonicalizeApiReferenceLinks(sidebar);
34+
2535
// --- Step 1: Group boundary rules ---
2636
const location = findCurrentPage(sidebar);
2737
if (location) {
@@ -81,10 +91,33 @@ export const onRequest = defineRouteMiddleware((context) => {
8191
interface SidebarEntry {
8292
type: string;
8393
label: string;
94+
href?: string;
8495
isCurrent?: boolean;
8596
entries?: SidebarEntry[];
8697
}
8798

99+
/**
100+
* Walk the sidebar tree and rewrite any link whose href is a localized API
101+
* reference path (e.g. `/it/reference/api/csharp/`) to the canonical,
102+
* locale-stripped path (e.g. `/reference/api/csharp/`).
103+
*/
104+
function canonicalizeApiReferenceLinks(entries: SidebarEntry[]): void {
105+
for (const entry of entries) {
106+
if (!entry) continue;
107+
108+
if (entry.type === 'link' && typeof entry.href === 'string') {
109+
const canonical = stripApiReferenceLocale(entry.href);
110+
if (canonical) {
111+
entry.href = canonical;
112+
}
113+
}
114+
115+
if (entry.entries) {
116+
canonicalizeApiReferenceLinks(entry.entries);
117+
}
118+
}
119+
}
120+
88121
/**
89122
* Recursively find the current page (`isCurrent === true`) in the sidebar tree
90123
* and return its sibling entries along with its index within them.

0 commit comments

Comments
 (0)