-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gatsby-ssr.js
40 lines (34 loc) · 1.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
import React from 'react';
import { CacheProvider } from '@emotion/react';
import createEmotionServer from '@emotion/server/create-instance';
import { renderToString } from 'react-dom/server';
import { makeMuiCache, makeTssCache } from './src/theme-cache';
import Layout from './src/components/layout';
import { TssCacheProvider } from 'tss-react';
export const replaceRenderer = (args) => {
const { bodyComponent, replaceBodyHTMLString, setHeadComponents, pathname } = args;
const muiCache = makeMuiCache();
const { extractCriticalToChunks } = createEmotionServer(muiCache);
const emotionStyles = extractCriticalToChunks(
renderToString(
<CacheProvider value={muiCache}>
<TssCacheProvider value={makeTssCache()}>
<Layout pathname={pathname}>{bodyComponent}</Layout>
</TssCacheProvider>
</CacheProvider>
)
);
const muiStyleTags = emotionStyles.styles.map((style) => {
const { css, key, ids } = style || {};
return (
<style
key={key}
data-emotion={`${key} ${ids.join(` `)}`}
dangerouslySetInnerHTML={{ __html: css }}
/>
);
});
setHeadComponents(muiStyleTags);
// render the result from `extractCritical`
replaceBodyHTMLString(emotionStyles.html);
};