Skip to content

Commit 3ead1d1

Browse files
committed
Add hreflang links for multilingual support in Header component
1 parent ae6831a commit 3ead1d1

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

components/Header.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import Head from 'next/head'
22
import { useRouter } from 'next/router'
33
import { useIntl } from 'react-intl'
44

5+
const BASE_URL = 'https://explorer.ooni.org'
6+
const LOCALES = JSON.parse(process.env.LOCALES || '["en"]')
7+
58
const Header = () => {
69
const { asPath, locale, defaultLocale } = useRouter()
710

11+
const path = asPath.split('?')[0]
812
const lang = locale === defaultLocale ? '' : `/${locale}`
9-
const canonical = `https://explorer.ooni.org${lang}${asPath.split('?')[0]}`
13+
const canonical = `${BASE_URL}${lang}${path}`
1014

1115
const intl = useIntl()
1216
const description = intl.formatMessage({ id: 'Home.Meta.Description' })
@@ -50,6 +54,19 @@ const Header = () => {
5054
<link rel="canonical" key="canonical" href={canonical} />
5155
<meta property="og:url" key="og:url" content={canonical} />
5256

57+
{LOCALES.map((loc) => {
58+
const prefix = loc === defaultLocale ? '' : `/${loc}`
59+
return (
60+
<link
61+
key={`hreflang-${loc}`}
62+
rel="alternate"
63+
hrefLang={loc}
64+
href={`${BASE_URL}${prefix}${path}`}
65+
/>
66+
)
67+
})}
68+
<link rel="alternate" hrefLang="x-default" href={`${BASE_URL}${path}`} />
69+
5370
<meta name="twitter:card" content="summary_large_image" />
5471
<meta key="twitter:title" name="twitter:title" content="OONI Explorer" />
5572
<meta name="twitter:site" content="@openobservatory" />

0 commit comments

Comments
 (0)