-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgatsby-ssr.js
38 lines (34 loc) · 1013 Bytes
/
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
/*
* Package Import
*/
import React from 'react';
import { extractCritical } from 'emotion-server';
import { renderToString } from 'react-dom/server';
/*
* Local Import
*/
/**
* Replace renderer • Replace the default server renderer.
* This is useful for integration with Redux, CSS-in-JS libraries, etc...
* That need custom setups for server rendering.
*
* @api : https://next.gatsbyjs.org/docs/ssr-apis/#replaceRenderer
*/
export const replaceRenderer = ({
bodyComponent,
replaceBodyHTMLString,
setHeadComponents,
}) => {
const ConnectedBody = () => <>{bodyComponent}</>;
const { html, ids, css } = extractCritical(renderToString(<ConnectedBody />));
const criticalStyle = <style dangerouslySetInnerHTML={{ __html: css }} />;
const criticalIds = (
<script
dangerouslySetInnerHTML={{
__html: `window.__EMOTION_CRITICAL_CSS_IDS__ = ${JSON.stringify(ids)};`,
}}
/>
);
setHeadComponents([criticalIds, criticalStyle]);
replaceBodyHTMLString(html);
};