forked from ti-broish/public
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderPage.js
36 lines (32 loc) · 1.11 KB
/
renderPage.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
import React from 'react';
import ReactDOMServer from 'react-dom/server';
import { ChunkExtractor } from '@loadable/server';
import path from 'path';
import Helmet from 'react-helmet';
import App from './src/components/App.js';
import { ServerStyleSheet } from 'styled-components';
export default renderData => {
const sheet = new ServerStyleSheet();
const statsFile = path.resolve('./public/bundles/loadable-stats.json')
const extractor = new ChunkExtractor({ statsFile });
try {
const html = ReactDOMServer.renderToString(
sheet.collectStyles(extractor.collectChunks(<App renderData={renderData}/>))
);
const headTags = Helmet.renderStatic();
const scriptTags = extractor.getScriptTags();
const linkTags = extractor.getLinkTags();
const styleTags = sheet.getStyleTags();
return {
html: html,
headTags: headTags,
scriptTags: scriptTags,
linkTags: linkTags,
styleTags: styleTags,
};
} catch (error) {
console.error(error);
} finally {
sheet.seal();
}
};