git ssb

1+

dinoworm ๐Ÿ› / catstack



Tree: b11662b22c32dc363fff6ac0ecf64b05f8733f00

Files: b11662b22c32dc363fff6ac0ecf64b05f8733f00 / app / render.js

2454 bytesRaw
1// https://github.com/jlongster/react-redux-universal-hot-example/blob/master/src/server.js
2
3import http from 'http'
4import Url from 'url'
5import React from 'react'
6import { renderToString } from 'react-dom/server'
7import { Provider } from 'react-redux'
8import { createHistory } from 'history'
9import { Router, RoutingContext, match } from 'react-router'
10import sendHtml from 'send-data/html'
11import sendError from 'send-data/error'
12import redirect from 'predirect'
13
14import createStore from 'app/store'
15import createRoutes from 'app/routes'
16import fetchAllData from 'app/util/fetch-all-data'
17
18export 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