-
Notifications
You must be signed in to change notification settings - Fork 17
/
gatsby-ssr.tsx
111 lines (101 loc) · 3.91 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**
* Implement Gatsby's SSR (Server Side Rendering) APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/ssr-apis/
*/
// You can delete this file if you're not using it
import React from 'react'
import type { GatsbySSR } from 'gatsby'
import * as fs from 'fs'
import { GlobalStyle } from './src/styles/global'
import { CookieConsentContextWrapper } from './src/analytics/contextual-cookie-consent'
import { thirdPartyProxyPath } from 'gatsby/dist/internal-plugins/partytown/proxy'
import { partytownAllowedHosts, partytownEnabled } from './src/partytown'
import { i18nForPageContext, withI18next } from './src/i18n'
export const wrapPageElement: GatsbySSR['wrapPageElement'] = ({ element, props: { pageContext } }) => {
return withI18next(i18nForPageContext(pageContext))(
<>
<GlobalStyle />
{element}
</>
)
}
export const wrapRootElement: GatsbySSR['wrapRootElement'] = ({ element }) => {
const visibleByDefault = process.env.COOKIE_CONSENT_EAGER_RENDER_ENABLED === 'true'
return <CookieConsentContextWrapper visibleByDefault={visibleByDefault}>{element}</CookieConsentContextWrapper>
}
export const onRenderBody: GatsbySSR['onRenderBody'] = ({ setHeadComponents }, options) => {
const files = getFilesFromPath('./public/static', '.woff2')
const preload = [
'montserrat-v24-latin-ext_latin-regular',
'montserrat-v24-latin-ext_latin-600',
'montserrat-v23-latin-700',
'montserrat-v23-latin-800',
'montserrat-v23-latin-ext_latin-900',
'lato-v23-latin-ext_latin-regular',
'lato-v23-latin-ext_latin-700',
'lato-v23-latin-ext_latin-900',
]
setHeadComponents([
// ...files.map((file, i) => {
// return preload.map((font, key) => {
// const fileBeginning = file.split('-').slice(0, -1).join('-')
// if (fileBeginning === font) {
// return (
// <link
// key={key}
// rel='preload'
// as='font'
// type='font/woff2'
// crossOrigin='anonymous'
// href={`/static/${file}`}
// />
// )
// } else {
// return null
// }
// })
// }),
partytownEnabled ? (
<script
key='partytown-vanilla-config'
dangerouslySetInnerHTML={{
__html: `
partytown = {
debug: false,
set(opts){
// https://github.com/BuilderIO/partytown/issues/72#issuecomment-1383790146
let sessionStorage = opts.window && opts.window.sessionStorage;
let isGtmTagDefinedInUrl = opts.window && opts.window.location && opts.window.location.search.includes("gtm_debug");
let isGtmTagDefinedInSessionStorage = sessionStorage && sessionStorage.getItem('isConnectedToGtagDebugger') == 'true'
let isDebugging = isGtmTagDefinedInUrl || isGtmTagDefinedInSessionStorage;
if ( isDebugging && opts.name === "type" && opts.nodeName === "SCRIPT" ) {
return opts.prevent;
} {
return opts.continue;
}
},
resolveUrl(url, location) {
let allowedHosts = new Set(${JSON.stringify(partytownAllowedHosts)});
if (allowedHosts.has(url.hostname)) {
// Use a secure connection
if (url?.protocol === 'http:') {
url = new URL(url.href.replace('http', 'https'))
}
// Point to our proxied URL
const proxyUrl = new URL(location.origin + '${thirdPartyProxyPath}')
proxyUrl.searchParams.append('url', url)
return proxyUrl
}
return url
}
}`,
}}
/>
) : null,
])
}
function getFilesFromPath(path: string, extension: string) {
let dir = fs.readdirSync(path)
return dir.filter(elm => elm.match(new RegExp(`.*\.(${extension})`, 'ig')))
}