-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-ssr.tsx
81 lines (78 loc) · 2.6 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as React from 'react'
import { wrapRootElement as wrap } from './wrap-root-element'
import { AnimatePresence } from 'framer-motion'
import { Partytown } from '@builder.io/partytown/react'
export function wrapPageElement({ element }) {
return (
<AnimatePresence mode="wait" initial={false}>
{element}
</AnimatePresence>
)
}
export const wrapRootElement = wrap
const ORIGIN = 'https://www.googletagmanager.com'
const GATSBY_GA_MEASUREMENT_ID = 'GTM-WLCMLLP'
export function onRenderBody({ setHtmlAttributes, setHeadComponents, setPreBodyComponents }) {
if (process.env.NODE_ENV !== 'production' && process.env.NODE_ENV !== 'test') return null
setHtmlAttributes({ lang: 'en' })
setHeadComponents([
<Partytown key="partytown" forward={['gtag']} />,
<script key="google-analytics" type="text/partytown" src={`${ORIGIN}/gtag/js?id=${GATSBY_GA_MEASUREMENT_ID}`} />,
<script
key="gtag"
type="text/partytown"
dangerouslySetInnerHTML={{
__html: `window.dataLayer = window.dataLayer || [];
window.gtag = function gtag(){ window.dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '${GATSBY_GA_MEASUREMENT_ID}', { send_page_view: false })`,
}}
/>,
])
setPreBodyComponents([
React.createElement('script', {
key: 'gatsby-dark-mode',
dangerouslySetInnerHTML: {
__html: `
void function() {
window.__onThemeChange = function() {}
var preferredTheme
try {
preferredTheme = localStorage.getItem('theme')
} catch (err) { }
function setTheme(newTheme) {
if (preferredTheme && document.documentElement.classList.contains(preferredTheme)) {
document.documentElement.classList.replace(preferredTheme, newTheme)
} else {
document.documentElement.classList.add(newTheme)
}
window.__theme = newTheme
preferredTheme = newTheme
window.__onThemeChange(newTheme)
}
window.__setPreferredTheme = function(newTheme) {
setTheme(newTheme)
try {
localStorage.setItem('theme', newTheme)
} catch (err) {}
}
var darkQuery = window.matchMedia('(prefers-color-scheme: dark)')
darkQuery.addListener(function(e) {
window.__setPreferredTheme(e.matches ? 'dark' : 'light')
})
setTheme(preferredTheme || (darkQuery.matches ? 'dark' : 'light'))
}()
`,
},
}),
<noscript
key="gtm"
dangerouslySetInnerHTML={{
__html: `
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-WLCMLLP" height="0" width="0"
style="display:none;visibility:hidden"></iframe>
`,
}}
/>,
])
}