-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-ssr.tsx
75 lines (71 loc) · 2.46 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
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
import React, { createElement } from 'react';
import { GatsbySSR } from 'gatsby';
import { darkColors, lightColors } from '@/styles/theme.css';
const colorThemeScript = `
(function() {
window.__onThemeChange = function() {};
function setTheme(newTheme) {
window.__theme = newTheme;
preferredTheme = newTheme;
document.documentElement.setAttribute('data-theme', newTheme);
document.documentElement.className = newTheme === 'dark' ? '${darkColors}' : '${lightColors}';
window.__onThemeChange(newTheme);
}
var preferredTheme;
try {
preferredTheme = localStorage.getItem('theme');
} catch (err) {}
window.__setPreferredTheme = function(newTheme) {
setTheme(newTheme);
try {
localStorage.setItem('theme', newTheme);
} catch (err) {}
}
var darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
darkQuery.addListener(function(e) {
window.__setPreferredTheme(e.matches ? 'dark' : 'light');
});
setTheme(preferredTheme || (darkQuery.matches ? 'dark' : 'light'));
})();
`;
export const onRenderBody: GatsbySSR['onRenderBody'] = ({
setHeadComponents,
setPreBodyComponents,
}) => {
setHeadComponents([
<link rel="preconnect" href="https://fonts.googleapis.com" key="fontsGoogle" />,
<link
rel="preconnect"
href="https://fonts.gstatic.com"
crossOrigin="anonymous"
key="fontsGstatic"
/>,
<link
href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=swap"
rel="stylesheet"
key="robotoMonoFont"
/>,
<link
rel="preload"
href="/fonts/PretendardVariable.woff2"
as="font"
type="font/woff2"
crossOrigin="anonymous"
key="pretendardFont"
/>,
<link rel="icon" type="image/svg+xml" href="/assets/favicon/favicon.svg" key="svgFavicon" />,
<link rel="icon" type="image/png" href="/assets/favicon/favicon.png" key="pngFavicon" />,
<meta property="og:image" content="/assets/images/thumbnail.png" key="ogImage" />,
<meta property="og:image:alt" content="semantic" key="ogImageAlt" />,
<meta name="twitter:image" content="/assets/images/thumbnail.png" key="twitterImage" />,
<meta name="twitter:image:alt" content="semantic" key="twitterImageAlt" />,
]);
setPreBodyComponents([
createElement('script', {
key: 'colorThemeScript',
dangerouslySetInnerHTML: {
__html: colorThemeScript,
},
}),
]);
};