Files: b11662b22c32dc363fff6ac0ecf64b05f8733f00 / app / render.js
2454 bytesRaw
1 | // https://github.com/jlongster/react-redux-universal-hot-example/blob/master/src/server.js |
2 | |
3 | import http from 'http' |
4 | import Url from 'url' |
5 | import React from 'react' |
6 | import { renderToString } from 'react-dom/server' |
7 | import { Provider } from 'react-redux' |
8 | import { createHistory } from 'history' |
9 | import { Router, RoutingContext, match } from 'react-router' |
10 | import sendHtml from 'send-data/html' |
11 | import sendError from 'send-data/error' |
12 | import redirect from 'predirect' |
13 | |
14 | import createStore from 'app/store' |
15 | import createRoutes from 'app/routes' |
16 | import fetchAllData from 'app/util/fetch-all-data' |
17 | |
18 | export function createServer (config) { |
19 | const staticUrl = Url.format(config.static.url) |
20 | |
21 | return http.createServer(render) |
22 | |
23 | function render (req, res) { |
24 | const store = createStore() |
25 | |
26 | match({ |
27 | routes: createRoutes(store), |
28 | location: req.url |
29 | }, function (err, redirectLocation, renderProps) { |
30 | if (redirectLocation) { |
31 | redirect(req, res, redirectLocation.pathname + redirectLocation.search) |
32 | } else if (err) { |
33 | sendError(req, res, { body: err }) |
34 | } else if (!renderProps) { |
35 | sendError(req, res, { |
36 | statusCode: 404, |
37 | body: new Error('Not found') |
38 | }) |
39 | } else { |
40 | fetchAllData( |
41 | renderProps.components, |
42 | store.getState, store.dispatch, |
43 | renderProps.location, |
44 | renderProps.params |
45 | ).then(function () { |
46 | const component = <Provider store={store}> |
47 | <RoutingContext { ...renderProps } /> |
48 | </Provider> |
49 | |
50 | var innerHtml |
51 | try { |
52 | innerHtml = renderToString(component) |
53 | } catch (err) { |
54 | return sendError(req, res, { body: err }) |
55 | } |
56 | |
57 | const html = renderFullPage(innerHtml, store.getState(), config) |
58 | |
59 | sendHtml(req, res, html) |
60 | }) |
61 | } |
62 | }) |
63 | } |
64 | |
65 | function renderFullPage (innerHtml, initialData) { |
66 | return ` |
67 | <!DOCTYPE html> |
68 | <html lang="en"> |
69 | <head> |
70 | <meta charset="utf-8" /> |
71 | <title>Craftworks TodoMVC</title> |
72 | <meta name="viewport" content="width=device-width, initial-scale=1" /> |
73 | </head> |
74 | <body> |
75 | <main>${ innerHtml }</main> |
76 | <script> |
77 | window.__data = ${ JSON.stringify(initialData) } |
78 | </script> |
79 | <script src="${Url.resolve(staticUrl, 'bundle.js')}"></script> |
80 | </body> |
81 | </html> |
82 | ` |
83 | } |
84 | } |
85 | |
86 |
Built with git-ssb-web