Skip to content

Commit 1fc3426

Browse files
render-server.js shouldn't depend on React/Redux-specific concepts
1 parent 05905a4 commit 1fc3426

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

samples/react/MusicStore/ReactApp/boot-server.tsx

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,33 @@ import * as React from 'react';
22
import { Provider } from 'react-redux';
33
import { renderToString } from 'react-dom/server';
44
import { match, RouterContext } from 'react-router';
5+
import createMemoryHistory from 'history/lib/createMemoryHistory';
56
React;
67

78
import { routes } from './routes';
89
import configureStore from './configureStore';
910
import { ApplicationState } from './store';
1011

11-
export default function (params: any, callback: (err: any, result: { html: string, store: Redux.Store }) => void) {
12-
match({ routes, location: params.location }, (error, redirectLocation, renderProps: any) => {
12+
export default function (params: any, callback: (err: any, result: { html: string, state: any }) => void) {
13+
const { location } = params;
14+
match({ routes, location }, (error, redirectLocation, renderProps: any) => {
1315
try {
1416
if (error) {
1517
throw error;
1618
}
1719

18-
const store = configureStore(params.history, params.state);
19-
const html = renderToString(
20+
const history = createMemoryHistory(params.url);
21+
const store = params.state as Redux.Store || configureStore(history);
22+
let html = renderToString(
2023
<Provider store={ store }>
2124
<RouterContext {...renderProps} />
2225
</Provider>
2326
);
27+
28+
// Also serialise the Redux state so the client can pick up where the server left off
29+
html += `<script>window.__redux_state = ${ JSON.stringify(store.getState()) }</script>`;
2430

25-
callback(null, { html, store });
31+
callback(null, { html, state: store });
2632
} catch (error) {
2733
callback(error, null);
2834
}

samples/react/MusicStore/ReactApp/fx/render-server.js

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
var createMemoryHistory = require('history/lib/createMemoryHistory');
21
var url = require('url');
32
var babelCore = require('babel-core');
43
var babelConfig = {
@@ -26,10 +25,9 @@ var domainTasks = require('./domain-tasks.js');
2625
var bootServer = require('../boot-server.jsx').default;
2726

2827
function render(requestUrl, callback) {
29-
var store;
3028
var params = {
3129
location: url.parse(requestUrl),
32-
history: createMemoryHistory(requestUrl),
30+
url: requestUrl,
3331
state: undefined
3432
};
3533

@@ -44,22 +42,16 @@ function render(requestUrl, callback) {
4442
} else {
4543
// The initial 'loading' state HTML is irrelevant - we only want to capture the state
4644
// so we can use it to perform a real render once all data is loaded
47-
store = result.store;
45+
params.state = result.state;
4846
resolve();
4947
}
5048
});
5149
}));
5250
}).then(function() {
5351
// By now, all the data should be loaded, so we can render for real based on the state now
54-
params.state = store.getState();
55-
bootServer(params, function(error, result) {
56-
if (error) {
57-
callback(error, null);
58-
} else {
59-
var html = result.html + `<script>window.__INITIAL_STATE = ${ JSON.stringify(store.getState()) }</script>`;
60-
callback(null, html)
61-
}
62-
});
52+
// TODO: Add an optimisation where, if domain-tasks had no outstanding tasks at the end of
53+
// the previous render, we don't re-render (we can use the previous html and state).
54+
bootServer(params, callback);
6355
}).catch(function(error) {
6456
process.nextTick(() => { // Because otherwise you can't throw from inside a catch
6557
callback(error, null);

0 commit comments

Comments
 (0)