-
Notifications
You must be signed in to change notification settings - Fork 9
/
gatsby-ssr.js
34 lines (33 loc) · 1.22 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
import React from "react"
export const onRenderBody = ({ setHeadComponents, setHtmlAttributes }) => {
setHeadComponents ([
<script
key="darkmode"
dangerouslySetInnerHTML={{
__html: `(function() {
function setTheme(theme) {
window.__theme = theme;
if (theme === 'dark') {
document.documentElement.className = 'dark';
} else {
document.documentElement.className = 'light';
}
};
window.__setPreferredTheme = function(theme) {
setTheme(theme);
try {
localStorage.setItem('theme', theme);
} catch (e) {}
};
let preferredTheme;
try {
preferredTheme = localStorage.getItem('theme');
} catch (e) {}
let darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
setTheme(preferredTheme || (darkQuery.matches ? 'dark' : 'light'));
})();`,
}}
/>,
])
setHtmlAttributes({ lang: `en` })
}