Skip to content

Commit 9acee3f

Browse files
committed
Fall back when a translated page is missing
1 parent 371a337 commit 9acee3f

2 files changed

Lines changed: 66 additions & 5 deletions

File tree

‎src/components/MDX/MDXComponents.tsx‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import type {Toc, TocItem} from './TocContext';
4242
import {TeamMember} from './TeamMember';
4343
import {LanguagesContext} from './LanguagesContext';
4444
import {finishedTranslations} from 'utils/finishedTranslations';
45+
import {ExternalLink} from 'components/ExternalLink';
4546

4647
import ErrorDecoder from './ErrorDecoder';
4748
import {IconCanary} from '../Icon/IconCanary';
@@ -486,14 +487,19 @@ function LanguageList({progress}: {progress: TranslationProgress}) {
486487
return (
487488
<UL>
488489
{languages.map(({code, name, enName}) => {
490+
const href =
491+
progress === 'complete' && pathname
492+
? `/api/translation?language=${encodeURIComponent(
493+
code
494+
)}&from=${encodeURIComponent(pathname)}`
495+
: `https://${code}.react.dev/`;
489496
return (
490497
<LI key={code}>
491-
<Link
492-
href={`https://${code}.react.dev${
493-
progress === 'complete' ? pathname || '/' : '/'
494-
}`}>
498+
<ExternalLink
499+
href={href}
500+
className="inline text-link dark:text-link-dark border-b border-link border-opacity-0 hover:border-opacity-100 duration-100 ease-in transition leading-normal">
495501
{enName} ({name})
496-
</Link>{' '}
502+
</ExternalLink>{' '}
497503
&mdash;{' '}
498504
<Link href={`https://github.com/reactjs/${code}.react.dev`}>
499505
Contribute

‎src/pages/api/translation.ts‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import type {NextApiRequest, NextApiResponse} from 'next';
9+
import {finishedTranslations} from 'utils/finishedTranslations';
10+
11+
export default async function handler(
12+
req: NextApiRequest,
13+
res: NextApiResponse
14+
) {
15+
if (req.method !== 'GET') {
16+
return res.status(405).end();
17+
}
18+
19+
const {language, from} = req.query;
20+
if (
21+
typeof language !== 'string' ||
22+
language === 'en' ||
23+
!finishedTranslations.includes(language)
24+
) {
25+
return res.status(404).end();
26+
}
27+
28+
const homepage = `https://${language}.react.dev/`;
29+
if (
30+
typeof from !== 'string' ||
31+
!/^\/(?!\/)(?:learn|reference)(?:\/|$)[^\\?#]*$/.test(from)
32+
) {
33+
return res.redirect(302, homepage);
34+
}
35+
36+
const translatedPage = new URL(from, homepage).toString();
37+
const controller = new AbortController();
38+
const timeout = setTimeout(() => controller.abort(), 2000);
39+
40+
try {
41+
const response = await fetch(translatedPage, {
42+
method: 'HEAD',
43+
signal: controller.signal,
44+
});
45+
if (response.ok) {
46+
return res.redirect(302, translatedPage);
47+
}
48+
} catch {
49+
// Keep the original homepage behavior when the translation site is unavailable.
50+
} finally {
51+
clearTimeout(timeout);
52+
}
53+
54+
return res.redirect(302, homepage);
55+
}

0 commit comments

Comments
 (0)