-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssr.js
37 lines (30 loc) · 952 Bytes
/
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
import App from './src/App';
import ReactDOMServer from 'react-dom/server';
import { AppRegistry } from 'react-native';
const fs = require('fs');
const indexHTML = fs.readFileSync('./build/index.html');
// register the app
AppRegistry.registerComponent('App', () => App);
const INITIAL_PROPS = {
name: 'GingerBread Man'
};
// prerender the app
const { element, getStyleElement } = AppRegistry.getApplication('App', {
initialProps: INITIAL_PROPS
});
const html = ReactDOMServer.renderToString(element);
const css = ReactDOMServer.renderToStaticMarkup(getStyleElement());
const initialPropsInject = `<script>window.INITIAL_PROPS = ${JSON.stringify(
INITIAL_PROPS
)}</script>`;
function serve(req, res, next) {
// construct HTML document string
const document = indexHTML
.toString()
.replace(
'<div id="root">',
'<div id="root">' + initialPropsInject + css + html
);
res.send(document);
}
export default serve;