-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-ssr.js
40 lines (35 loc) · 1.05 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
/**
* Implement Gatsby's SSR (Server Side Rendering) APIs in this file.
*
* See: https://www.gatsbyjs.org/docs/ssr-apis/
*/
// From https://github.com/mui-org/material-ui/blob/master/examples/gatsby/gatsby-ssr.js#L6
const React = require('react');
const { renderToString } = require('react-dom/server');
const JssProvider = require('react-jss').default;
const getPageContext = require('./src/components/withCss').getPageContext;
function replaceRenderer({
bodyComponent,
replaceBodyHTMLString,
setHeadComponents,
}) {
// Get the context of the page to collected side effects.
const muiPageContext = getPageContext();
const bodyHTML = renderToString(
<JssProvider registry={muiPageContext.sheetsRegistry}>
{bodyComponent}
</JssProvider>
);
replaceBodyHTMLString(bodyHTML);
setHeadComponents([
<style
type="text/css"
id="jss-server-side"
key="jss-server-side"
dangerouslySetInnerHTML={{
__html: muiPageContext.sheetsRegistry.toString(),
}}
/>,
]);
}
exports.replaceRenderer = replaceRenderer;