-
Notifications
You must be signed in to change notification settings - Fork 11
/
gatsby-ssr.tsx
44 lines (37 loc) · 1.19 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
import Context from '@/contexts'
import { isProd } from '@/utils/environment'
import Layout from '@/layouts'
import type { GatsbySSR } from 'gatsby'
import type { PageContext } from 'config/types'
import { ChunkExtractor } from '@loadable/server'
import { resolve } from 'path'
const extractor = new ChunkExtractor({
statsFile: resolve('./.cache/loadable-stats-build-javascript.json'),
entrypoints: []
})
const wrapRootElement: GatsbySSR['wrapRootElement'] = ({ element }) => {
return <Context>{extractor.collectChunks(element)}</Context>
}
const onRenderBody: GatsbySSR['onRenderBody'] = ({ setHeadComponents }) => {
if (!isProd) {
return
}
setHeadComponents([
<script
key="google-adsense"
async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3149742411805247"
crossOrigin="anonymous"
/>,
<script key="google-ads">
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
])
}
const wrapPageElement: GatsbySSR<
Record<string, unknown>,
PageContext
>['wrapPageElement'] = ({ props, element }) => {
return <Layout {...props}>{element}</Layout>
}
export { wrapRootElement, wrapPageElement, onRenderBody }