-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-ssr.js
44 lines (38 loc) · 1.21 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
/**
* Implement Gatsby's SSR (Server Side Rendering) APIs in this file.
*
* See: https://www.gatsbyjs.com/docs/ssr-apis/
*/
const React = require('react')
const PreBodyComponents = [
<script
key="dark"
dangerouslySetInnerHTML={{
__html: `(function() {
function setTheme(theme) {
window.__theme = theme;
if (theme === 'dark') {
document.body.setAttribute('data-theme', 'dark')
} else {
document.body.setAttribute('data-theme', 'light')
}
};
window.__setPreferredTheme = function(theme) {
setTheme(theme);
try {
localStorage.setItem('color-theme', theme);
} catch (e) {}
};
let preferredTheme;
try {
preferredTheme = localStorage.getItem('color-theme');
} catch (e) {}
let darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
setTheme(preferredTheme || (darkQuery.matches ? 'dark' : 'light'));
})();`,
}}
/>,
]
exports.onRenderBody = ({ setPreBodyComponents }) => {
setPreBodyComponents(PreBodyComponents)
}