-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-ssr.tsx
42 lines (38 loc) · 1.27 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
import { RenderBodyArgs } from "gatsby";
import React from "react";
const setColorTheme = `
(function() {
const mode = localStorage.getItem('theme');
if (mode !== null && ['light', 'dark'].includes(mode)) {
document.documentElement.dataset.theme = mode;
return;
}
const preferredColorScheme = window.matchMedia('(prefers-color-scheme: dark)');
const hasMediaQueryPreference = typeof preferredColorScheme.matches === 'boolean';
if (hasMediaQueryPreference && preferredColorScheme.matches === true) {
document.documentElement.dataset.theme = 'dark';
} else {
document.documentElement.dataset.theme = 'light'
}
})();
`;
export const onRenderBody = ({
setPreBodyComponents,
setHeadComponents,
pathname,
}: RenderBodyArgs) => {
const currentUrl = `https://www.rickvanlieshout.com${pathname}`;
setHeadComponents([
<meta data-url="currentUrl" key="og:url" property="og:url" content={currentUrl} />,
<link data-url="currentUrl" key="canonical" rel="canonical" href={currentUrl} />,
<meta data-url="currentUrl" property="test:rick" key="test:rick" content={currentUrl} />,
]);
setPreBodyComponents([
React.createElement("script", {
key: "theme",
dangerouslySetInnerHTML: {
__html: setColorTheme,
},
}),
]);
};