generated from pixel-point/gatsby-tailwind-starter
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgatsby-ssr.js
61 lines (54 loc) · 2.01 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import React from 'react';
const SITE_DOMAIN = 'pixelpoint.io';
const PLAUSIBLE_DOMAIN = 'plausible.pixelpoint.io';
const SCRIPT_URI = '/js/plausible.js';
// eslint-disable-next-line import/prefer-default-export
export const onRenderBody = ({ setHeadComponents, setPreBodyComponents, setHtmlAttributes }) => {
if (process.env.NODE_ENV === 'production') {
const scriptProps = {
'data-domain': SITE_DOMAIN,
src: `https://${PLAUSIBLE_DOMAIN}${SCRIPT_URI}`,
};
setHeadComponents([
// eslint-disable-next-line react/jsx-filename-extension
<link key="plausible-preconnect" rel="preconnect" href={`https://${PLAUSIBLE_DOMAIN}`} />,
<script key="plausible-script" defer {...scriptProps} />,
// See: https://plausible.io/docs/custom-event-goals#1-trigger-custom-events-with-javascript-on-your-site
<script
key="plausible-custom-events"
dangerouslySetInnerHTML={{
__html: `
window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) };
`,
}}
/>,
]);
}
setPreBodyComponents([
<script
dangerouslySetInnerHTML={{
__html: `
(function() {
if(window.matchMedia('(prefers-color-scheme: dark)').matches) {
plausible('User Color Mode', { props: { mode: 'dark' } });
}
else {
plausible('User Color Mode', { props: { mode: 'light' } });
}
if (window.location.pathname.startsWith('/blog')) {
const isDarkModeSetInLocalStorage = typeof localStorage !== 'undefined' && localStorage.theme === 'dark';
const isSystemModeDark = !('theme' in localStorage) && typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches;
if (isDarkModeSetInLocalStorage || isSystemModeDark) {
document.documentElement.classList.add('dark');
} else {
document.documentElement.classList.remove('dark');
}
}
})()`,
}}
key="theme-picker"
/>,
]);
setHtmlAttributes({ lang: 'en' });
return null;
};