-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathonRenderBody.tsx
27 lines (24 loc) · 1.19 KB
/
onRenderBody.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { GatsbySSR } from 'gatsby';
import { SitePageContext, Translation } from '../types';
export const onRenderBody: GatsbySSR['onRenderBody'] = ({ loadPageDataSync, pathname, setHtmlAttributes, setHeadComponents }, options) => {
// loadPageDataSync is not implemented in all run modes like development
if (!loadPageDataSync) {
return;
}
const {
result: { pageContext },
} = loadPageDataSync(pathname) as { result: { pageContext: SitePageContext } };
const locale = pageContext.locale ?? (options.defaultLocale as string);
const siteUrl = new URL(options.siteUrl as string);
const translations = (pageContext.translations as Translation[]) || [];
setHtmlAttributes({ lang: locale });
setHeadComponents([
<link rel="alternate" hrefLang="x-default" href={siteUrl.href} />,
<link rel="alternate" hrefLang={locale} href={new URL(pathname, siteUrl).href} />,
<meta property="og:locale" content={locale.replace(`-`, `_`)} />,
...translations.map((t) => [
<link key={t.locale} rel="alternate" hrefLang={t.locale} href={new URL(t.path, siteUrl).href} />,
<meta key={t.locale} property="og:locale:alternate" content={t.locale.replace(`-`, `_`)} />,
]),
]);
};