git ssb

1+

dinoworm ๐Ÿ› / catstack



Tree: d257f1cb78aa333ab6399429df9d754ce4f561f5

Files: d257f1cb78aa333ab6399429df9d754ce4f561f5 / app / stack / render.js

1964 bytesRaw
1// https://github.com/jlongster/react-redux-universal-hot-example/blob/master/src/server.js
2
3const React = require('react')
4const { renderToString } = require('react-dom/server')
5const { Provider } = require('react-redux')
6const { createHistory } = require('history')
7const { Router, RoutingContext, match } = require('react-router')
8
9const createStore = require('app/store')
10const routes = require('app/routes')
11const fetchAllData = require('app/util/fetch-all-data')
12
13module.exports = createRender
14
15function createRender (config) {
16 return function render (req, res) {
17 const store = createStore()
18
19 match({
20 routes: routes,
21 location: req.path
22 }, function (err, redirectLocation, renderProps) {
23 if (redirectLocation) {
24 res.redirect(redirectLocation.pathname + redirectLocation.search)
25 } else if (err) {
26 res.status(500).send(err.message)
27 } else if (!renderProps) {
28 res.status(404).send('Not found')
29 } else {
30 fetchAllData(
31 renderProps.components,
32 store.getState, store.dispatch,
33 renderProps.location,
34 renderProps.params
35 ).then(function () {
36 const component = <Provider store={store} key="provider">
37 <RoutingContext { ...renderProps } />
38 </Provider>
39
40 const html = renderToString(component)
41
42 const fullHtml = renderFullPage(html, store.getState())
43
44 res.send(fullHtml)
45 })
46 }
47 })
48 }
49}
50
51function renderFullPage (html, data) {
52 return `
53 <!DOCTYPE html>
54 <html lang="en">
55 <head>
56 <meta charset="utf-8" />
57 <title>Production TodoMVC</title>
58 <meta name="viewport" content="width=device-width, initial-scale=1" />
59 </head>
60 <body>
61 <main>${ html }</main>
62 <script>
63 window.__data = ${ JSON.stringify(data) }
64 </script>
65 <script src="bundle.js"></script>
66 </body>
67 </html>
68 `
69}
70

Built with git-ssb-web