Skip to content

Commit

Permalink
Sync up react-router and redux on server (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardscarrott committed Mar 28, 2017
1 parent 121a96f commit 1ffe020
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import { renderToString, renderToStaticMarkup } from 'react-dom/server';
import { match, RouterContext } from 'react-router';
import { createMemoryHistory, match, RouterContext } from 'react-router';
import { syncHistoryWithStore } from 'react-router-redux';
import { Provider } from 'react-redux';
import Helmet from 'react-helmet';
import configureStore from 'store/configureStore';
Expand Down Expand Up @@ -90,16 +91,21 @@ export default stats => {
* @return {undefined} undefined
*/
return (req, res, next) => {
const url = req.url;
const memoryHistory = createMemoryHistory(url);
const store = configureStore();
const history = syncHistoryWithStore(memoryHistory, store);

match({
history,
routes,
location: req.url
location: url
}, (error, redirectLocation, renderProps) => {
if (error) {
res.status(500).send(error.message);
} else if (redirectLocation) {
res.redirect(302, `${redirectLocation.pathname}${redirectLocation.search}`);
} else {
const store = configureStore();
fetchComponentData(renderProps, store)
.then(() => {
let html;
Expand Down

0 comments on commit 1ffe020

Please sign in to comment.