Simplifications in ReactSpa and ReactReduxSpa

This commit is contained in:
Steve Sanderson
2017-05-17 12:44:12 +01:00
parent 785e7d48a2
commit e658ee6375
8 changed files with 28 additions and 28 deletions

View File

@@ -2,7 +2,7 @@ import * as React from 'react';
import { Provider } from 'react-redux';
import { renderToString } from 'react-dom/server';
import { StaticRouter } from 'react-router-dom';
import { replace } from "react-router-redux";
import { replace } from 'react-router-redux';
import { createMemoryHistory } from 'history';
import { createServerRenderer, RenderResult } from 'aspnet-prerendering';
import routes from './routes';
@@ -10,31 +10,28 @@ import configureStore from './configureStore';
export default createServerRenderer(params => {
return new Promise<RenderResult>((resolve, reject) => {
// Create memory history to use in the Redux store
const history = createMemoryHistory();
const store = configureStore(history);
// Dispatch the current location so that the router knows where to go
// Prepare Redux store with in-memory history, and dispatch a navigation event
// corresponding to the incoming URL
const store = configureStore(createMemoryHistory());
store.dispatch(replace(params.location));
const context : any = {};
// Prepare an instance of the application and perform an inital render that will
// cause any async tasks (e.g., data access) to begin
const routerContext: any = {};
const app = (
<Provider store={ store }>
<StaticRouter context={ context } location={ params.location.path } children={ routes } />
<StaticRouter context={ routerContext } location={ params.location.path } children={ routes } />
</Provider>
);
// Perform an initial render that will cause any async tasks (e.g., data access) to begin
renderToString(app);
// If there's a redirection, just send this information back to the host application (Maybe improve this?)
if (context.url) {
resolve({ redirectUrl: context.url });
// If there's a redirection, just send this information back to the host application
if (routerContext.url) {
resolve({ redirectUrl: routerContext.url });
return;
}
// Once the tasks are done, we can perform the final render
// Once any async tasks are done, we can perform the final render
// We also send the redux store state, so the client can continue execution where the server left off
params.domainTasks.then(() => {
resolve({