Skip to content

Commit 84af98d

Browse files
fix(i18n): handle double-prefixed legacy English routes
Generate additional static paths under /en/ for slugs that already contain the en-US/ prefix (e.g. /en/en-US/product-overview/) so they redirect to the canonical /en-US/ path. Add a toCanonicalEnglishPath helper to normalize redirect targets and extend i18n and langParamHandler test suites with double-prefix coverage. Co-Authored-By: Hagicode <noreply@hagicode.com> Signed-off-by: newbe36524 <newbe36524@qq.com>
1 parent 1ada4a6 commit 84af98d

3 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/lib/i18n.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ describe('docs locale helpers', () => {
115115

116116
it.each([
117117
['/en-US/product-overview/', '/en-US/product-overview/'],
118+
['/en/en-US/product-overview/', '/en-US/product-overview/'],
118119
['/en-US/ja-JP/product-overview/', '/ja-JP/product-overview/'],
119120
['/zh-CN/product-overview/', '/product-overview/'],
120121
['/ja/product-overview/', '/ja-JP/product-overview/'],

src/lib/langParamHandler.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ describe('docs language route resolution', () => {
176176
route('https://docs.hagicode.com/en-US/ja-JP/product-overview/', JSON.stringify({ lang: 'en-US' }), ['en-US'])
177177
.shouldRedirect,
178178
).toBe(true);
179+
180+
expect(route('https://docs.hagicode.com/en/en-US/product-overview/', null, ['en-US']).targetUrl).toBe(
181+
'https://docs.hagicode.com/en-US/product-overview/',
182+
);
183+
expect(route('https://docs.hagicode.com/en/en-US/product-overview/', null, ['en-US']).shouldRedirect).toBe(
184+
true,
185+
);
179186
});
180187
});
181188

src/pages/en/[...slug].astro

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ type LegacyEnglishRouteProps = {
99
redirectPath: string;
1010
};
1111
12+
function toCanonicalEnglishPath(route: string): string {
13+
const normalizedRoute = route.replace(/^en-US(?:\/|$)/u, '');
14+
return normalizedRoute.length > 0 ? `/en-US/${normalizedRoute}/` : '/en-US/';
15+
}
16+
1217
export async function getStaticPaths() {
1318
const docsRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../../..');
1419
const englishDocsRoot = path.join(docsRoot, 'src/content/docs/en-US');
@@ -45,16 +50,25 @@ export async function getStaticPaths() {
4550
};
4651
4752
const routes = await collectLegacyEnglishRoutes(englishDocsRoot);
53+
const legacyRoutes = new Set<string>();
54+
55+
for (const route of routes) {
56+
if (route.length === 0) {
57+
continue;
58+
}
59+
60+
legacyRoutes.add(route);
61+
legacyRoutes.add(`en-US/${route}`);
62+
}
4863
49-
return routes
50-
.filter((route) => route.length > 0)
64+
return Array.from(legacyRoutes)
5165
.sort((left, right) => left.localeCompare(right))
5266
.map((route) => ({
5367
params: {
5468
slug: route,
5569
},
5670
props: {
57-
redirectPath: `/en-US/${route}/`,
71+
redirectPath: toCanonicalEnglishPath(route),
5872
} satisfies LegacyEnglishRouteProps,
5973
}));
6074
}

0 commit comments

Comments
 (0)