-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-ssr.js
58 lines (50 loc) · 1.75 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
/* 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" />])
}
export const onPreRenderHTML = ({
getHeadComponents,
replaceHeadComponents,
}) => {
const headComponents = getHeadComponents()
// enforce the global style before the others
const orderedComponents = headComponents.sort((item) => {
const isGlobalStyle =
item.type === 'style' &&
item.props['data-href'] &&
/^\/styles.[a-zA-Z0-9]*.css$/.test(item.props['data-href'])
return isGlobalStyle ? -1 : 1
})
replaceHeadComponents(orderedComponents)
}