-
Notifications
You must be signed in to change notification settings - Fork 36
/
gatsby-ssr.js
125 lines (122 loc) · 4.67 KB
/
gatsby-ssr.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import React from 'react';
import { ThemeProvider } from '@emotion/react';
import { renderStylesToString } from '@leafygreen-ui/emotion';
import { renderToString } from 'react-dom/server';
import { theme } from './src/theme/docsTheme';
import EuclidCircularASemiBold from './src/styles/fonts/EuclidCircularA-Semibold-WebXL.woff';
import redirectBasedOnLang from './src/utils/head-scripts/redirect-based-on-lang';
import { getHtmlLangFormat } from './src/utils/locale';
import bindTabUI from './src/utils/head-scripts/offline-ui/tabs';
import { isOfflineDocsBuild } from './src/utils/is-offline-docs-build';
import updateSidenavHeight from './src/utils/head-scripts/offline-ui/sidenav';
export const onRenderBody = ({ setHeadComponents, setHtmlAttributes }) => {
const headComponents = [
// GTM Pathway
<script
key="pathway"
type="text/javascript"
dangerouslySetInnerHTML={{
__html: `!function(e,n){var t=document.createElement("script"),o=null,x="pathway";t.async=!0,t.src='https://'+x+'.mongodb.com/'+(e?x+'-debug.js':''),document.head.append(t),t.addEventListener("load",function(){o=window.pathway.default,(n&&o.configure(n)),o.createProfile("mongodbcom").load(),window.segment=o})}();`,
}}
/>,
// Client-side redirect based on browser's language settings
<script
key="browser-lang-redirect"
type="text/javascript"
dangerouslySetInnerHTML={{
// Call function immediately on load
__html: `!${redirectBasedOnLang}()`,
}}
/>,
<link
rel="preload"
href={EuclidCircularASemiBold}
as="font"
type="font/woff"
crossOrigin="anonymous"
key="euclidCircularSemiBold"
/>,
<link key="preconnectGoogleAPIS" rel="preconnect" href="https://fonts.googleapis.com" />,
<link key="preconnectGStatic" rel="preconnect" href="https://fonts.gstatic.com" crossOrigin="anonymous" />,
<link
key="notoSansCS"
href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:[email protected]&display=swap"
rel="stylesheet"
/>,
<link
key="notoSansKR"
href="https://fonts.googleapis.com/css2?family=Noto+Sans+KR:[email protected]&display=swap"
rel="stylesheet"
/>,
<link
key="notoSansJP"
href="https://fonts.googleapis.com/css2?family=Noto+Sans+JP:[email protected]&display=swap"
rel="stylesheet"
></link>,
];
if (process.env['GATSBY_ENABLE_DARK_MODE'] === 'true') {
// Detect dark mode
// before document body
// to prevent flash of incorrect theme
headComponents.push(
<script
key="dark-mode"
dangerouslySetInnerHTML={{
__html: `
!function () {
try {
var d = document.documentElement.classList;
d.remove("light-theme", "dark-theme");
var h = window.location.href;
if (h.includes('/openapi/preview')) return;
var e = JSON.parse(localStorage.getItem("mongodb-docs"))?.["theme"];
if ("system" === e || (!e)) {
var t = "(prefers-color-scheme: dark)",
m = window.matchMedia(t);
m.media !== t || m.matches ? d.add("dark-theme", "system") : d.add("light-theme", "system");
} else if (e) {
var x = { "light-theme": "light-theme", "dark-theme": "dark-theme" };
x[e] && d.add(x[e]);
}
} catch (e) {
console.error(e);
}
}();
`,
}}
/>
);
}
if (isOfflineDocsBuild) {
headComponents.push(
<script
key="offline-docs-ui"
type="text/javascript"
dangerouslySetInnerHTML={{
// Call function immediately on load
__html: `!${bindTabUI}()`,
}}
/>
);
headComponents.push(
<script
key="offline-sidenav-ui"
type="text/javascript"
dangerouslySetInnerHTML={{
// Call function immediately on load
__html: `!${updateSidenavHeight}()`,
}}
/>
);
}
setHtmlAttributes({
// Help work with translated content locally; Smartling should handle rewriting the lang
lang: process.env.GATSBY_LOCALE ? getHtmlLangFormat(process.env.GATSBY_LOCALE) : 'en',
});
setHeadComponents(headComponents);
};
// Support SSR for LeafyGreen components
// https://github.com/mongodb/leafygreen-ui/tree/master/packages/emotion#server-side-rendering
export const replaceRenderer = ({ replaceBodyHTMLString, bodyComponent }) =>
replaceBodyHTMLString(renderStylesToString(renderToString(bodyComponent)));
export const wrapRootElement = ({ element }) => <ThemeProvider theme={theme}>{element}</ThemeProvider>;