-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgatsby-ssr.tsx
41 lines (35 loc) · 1.36 KB
/
gatsby-ssr.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { GatsbySSR, RenderBodyArgs, WrapRootElementNodeArgs } from 'gatsby';
import React from 'react';
import { HelmetProvider, HelmetServerState } from 'react-helmet-async';
const context: { helmet?: HelmetServerState } = {};
export const onRenderBody: GatsbySSR['onRenderBody'] = ({
setHeadComponents,
setHtmlAttributes,
setBodyAttributes
}: RenderBodyArgs): void => {
const { helmet } = context;
if (helmet) {
const baseComponent = helmet.base.toComponent();
const titleComponent = helmet.title.toComponent() as unknown as any[];
const components = [
helmet.priority.toComponent(),
helmet.meta.toComponent(),
helmet.link.toComponent(),
helmet.style.toComponent(),
helmet.script.toComponent(),
helmet.noscript.toComponent()
];
setHeadComponents(
titleComponent[0]?.props.children
? [baseComponent, titleComponent, ...components]
: [baseComponent, ...components]
);
setHtmlAttributes(helmet.htmlAttributes.toComponent());
setBodyAttributes(helmet.bodyAttributes.toComponent());
}
};
export const wrapRootElement: GatsbySSR['wrapRootElement'] = ({
element
}: WrapRootElementNodeArgs): React.ReactElement => (
<HelmetProvider context={context}>{element}</HelmetProvider>
);