@@ -2,27 +2,33 @@ import * as React from 'react';
22import { Provider } from 'react-redux' ;
33import { renderToString } from 'react-dom/server' ;
44import { match , RouterContext } from 'react-router' ;
5+ import createMemoryHistory from 'history/lib/createMemoryHistory' ;
56React ;
67
78import { routes } from './routes' ;
89import configureStore from './configureStore' ;
910import { 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 }
0 commit comments