render-server.js shouldn't depend on React/Redux-specific concepts

This commit is contained in:
SteveSandersonMS
2016-02-08 10:08:35 -08:00
parent 05905a42ab
commit 1fc3426278
2 changed files with 16 additions and 18 deletions

View File

@@ -2,27 +2,33 @@ import * as React from 'react';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import { renderToString } from 'react-dom/server'; import { renderToString } from 'react-dom/server';
import { match, RouterContext } from 'react-router'; import { match, RouterContext } from 'react-router';
import createMemoryHistory from 'history/lib/createMemoryHistory';
React; React;
import { routes } from './routes'; import { routes } from './routes';
import configureStore from './configureStore'; import configureStore from './configureStore';
import { ApplicationState } from './store'; import { ApplicationState } from './store';
export default function (params: any, callback: (err: any, result: { html: string, store: Redux.Store }) => void) { export default function (params: any, callback: (err: any, result: { html: string, state: any }) => void) {
match({ routes, location: params.location }, (error, redirectLocation, renderProps: any) => { const { location } = params;
match({ routes, location }, (error, redirectLocation, renderProps: any) => {
try { try {
if (error) { if (error) {
throw error; throw error;
} }
const store = configureStore(params.history, params.state); const history = createMemoryHistory(params.url);
const html = renderToString( const store = params.state as Redux.Store || configureStore(history);
let html = renderToString(
<Provider store={ store }> <Provider store={ store }>
<RouterContext {...renderProps} /> <RouterContext {...renderProps} />
</Provider> </Provider>
); );
callback(null, { html, store }); // Also serialise the Redux state so the client can pick up where the server left off
html += `<script>window.__redux_state = ${ JSON.stringify(store.getState()) }</script>`;
callback(null, { html, state: store });
} catch (error) { } catch (error) {
callback(error, null); callback(error, null);
} }

View File

@@ -1,4 +1,3 @@
var createMemoryHistory = require('history/lib/createMemoryHistory');
var url = require('url'); var url = require('url');
var babelCore = require('babel-core'); var babelCore = require('babel-core');
var babelConfig = { var babelConfig = {
@@ -26,10 +25,9 @@ var domainTasks = require('./domain-tasks.js');
var bootServer = require('../boot-server.jsx').default; var bootServer = require('../boot-server.jsx').default;
function render(requestUrl, callback) { function render(requestUrl, callback) {
var store;
var params = { var params = {
location: url.parse(requestUrl), location: url.parse(requestUrl),
history: createMemoryHistory(requestUrl), url: requestUrl,
state: undefined state: undefined
}; };
@@ -44,22 +42,16 @@ function render(requestUrl, callback) {
} else { } else {
// The initial 'loading' state HTML is irrelevant - we only want to capture the state // The initial 'loading' state HTML is irrelevant - we only want to capture the state
// so we can use it to perform a real render once all data is loaded // so we can use it to perform a real render once all data is loaded
store = result.store; params.state = result.state;
resolve(); resolve();
} }
}); });
})); }));
}).then(function() { }).then(function() {
// By now, all the data should be loaded, so we can render for real based on the state now // By now, all the data should be loaded, so we can render for real based on the state now
params.state = store.getState(); // TODO: Add an optimisation where, if domain-tasks had no outstanding tasks at the end of
bootServer(params, function(error, result) { // the previous render, we don't re-render (we can use the previous html and state).
if (error) { bootServer(params, callback);
callback(error, null);
} else {
var html = result.html + `<script>window.__INITIAL_STATE = ${ JSON.stringify(store.getState()) }</script>`;
callback(null, html)
}
});
}).catch(function(error) { }).catch(function(error) {
process.nextTick(() => { // Because otherwise you can't throw from inside a catch process.nextTick(() => { // Because otherwise you can't throw from inside a catch
callback(error, null); callback(error, null);