-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgatsby-ssr.js
42 lines (37 loc) · 1.07 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
import React from "react"
import App from "src/root"
const darkModeScript = () => {
const mql = window.matchMedia("(prefers-color-scheme: dark)")
const prefersDarkScheme = mql.matches
const storedColorMode = localStorage.getItem("set-color-mode")
let colorMode
if (typeof storedColorMode === "string") {
colorMode = storedColorMode
} else {
colorMode = prefersDarkScheme ? "dark" : "light"
}
let root = document.documentElement
if (colorMode === "dark") root.classList.add("dark")
if (colorMode === "light") root.classList.remove("dark")
}
export const onRenderBody = ({
setPreBodyComponents,
setHeadComponents,
pathname,
}) => {
setHeadComponents([
<link key={0} rel="canonical" href={`https://otd.ink${pathname}`} />,
<meta key={1} property="og:url" content={`https://otd.ink${pathname}`} />,
])
setPreBodyComponents(
<script
key="no-flash"
dangerouslySetInnerHTML={{
__html: `(${String(darkModeScript)})()`,
}}
/>
)
}
export const wrapRootElement = ({ element }) => {
return <App>{element}</App>
}