-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-ssr.js
79 lines (69 loc) · 2.24 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* eslint-disable react/jsx-filename-extension */
import { CartProvider, SessionProvider, UIProvider } from '@faststore/sdk'
import React from 'react'
import ThirdPartyScripts from './src/components/ThirdPartyScripts'
import Layout from './src/Layout'
import AnalyticsHandler from './src/sdk/analytics'
import { validateCart } from './src/sdk/cart/validate'
import ErrorBoundary from './src/sdk/error/ErrorBoundary'
import TestProvider from './src/sdk/tests'
import { uiActions, uiEffects, uiInitialState } from './src/sdk/ui'
import storeConfig from './store.config'
export const wrapRootElement = ({ element }) => (
<ErrorBoundary>
<AnalyticsHandler />
<TestProvider>
<UIProvider
initialState={uiInitialState}
actions={uiActions}
effects={uiEffects}
>
<SessionProvider initialState={{ channel: storeConfig.channel }}>
<CartProvider mode="optimistic" onValidateCart={validateCart}>
{element}
</CartProvider>
</SessionProvider>
</UIProvider>
</TestProvider>
</ErrorBoundary>
)
export const wrapPageElement = ({ element }) => {
return <Layout>{element}</Layout>
}
export const onRenderBody = ({ setHeadComponents }) => {
setHeadComponents([<ThirdPartyScripts key="ThirdPartyScripts" />])
}
/**
* Gatsby inlines all styles from the app inside a `<style/>` tag. This decreases
* FCP, but increases TBT. Since we are having trouble with TBT, replacing `<style/>`
* with `<link/>` tags should lower TBT. This switch, however is not supported by
* Gatsby.
* A workaround described in https://github.com/gatsbyjs/gatsby/issues/1526 is
* implemented below
*/
export const onPreRenderHTML = ({
getHeadComponents,
replaceHeadComponents,
}) => {
if (process.env.NODE_ENV !== 'production') {
return
}
const transformedHeadComponents = getHeadComponents().map((node) => {
if (node.type === 'style') {
const globalStyleHref = node.props['data-href']
if (globalStyleHref) {
return (
<link
href={globalStyleHref}
rel="stylesheet"
type="text/css"
media="screen"
/>
)
}
return node
}
return node
})
replaceHeadComponents(transformedHeadComponents)
}