-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-ssr.tsx
63 lines (55 loc) · 1.77 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* *** MIT LICENSE ***
* -------------------------------------------------------------------------
* This code may be modified and distributed under the MIT license.
* See the LICENSE file for details.
* -------------------------------------------------------------------------
*
* @summary The code below specifies the configuration
* related to the server-side rendering.
*
* See https://www.gatsbyjs.org/docs/ssr-apis
* for defailed usage
*
* @author Alvis HT Tang <[email protected]>
* @license MIT
* @copyright Copyright (c) 2018 - All Rights Reserved.
* -------------------------------------------------------------------------
*/
import { renderStaticOptimized } from 'glamor/server';
import React from 'react';
import { renderToString } from 'react-dom/server';
import { Provider } from 'react-redux';
import createStore from '#redux';
import SSR from '#definitions/gatsby/ssr';
// export const onRenderBody: SSR.onRenderBody = (parameters): void => {};
// fix https://github.com/gatsbyjs/gatsby/issues/3231
export const replaceRenderer: SSR.replaceRenderer = ({
bodyComponent,
replaceBodyHTMLString,
setHeadComponents
}): void => {
const { store } = createStore();
const { html, css, ids } = renderStaticOptimized(() =>
renderToString(<Provider store={store}>{bodyComponent}</Provider>)
);
replaceBodyHTMLString(html);
setHeadComponents([
<style
id="glamor-styles"
key="glamor-styles"
dangerouslySetInnerHTML={{ __html: css }}
/>,
<script
id="glamor-ids"
key="glamor-ids"
dangerouslySetInnerHTML={{
__html: `
// <![CDATA[
window._glamor = ${JSON.stringify(ids)}
// ]]>
`
}}
/>
]);
};