Skip to content

Instantly share code, notes, and snippets.

@jacobp100
Last active July 16, 2016 21:18
Show Gist options
  • Select an option

  • Save jacobp100/2db9e1aa39b9ffd1d396479ae13c056b to your computer and use it in GitHub Desktop.

Select an option

Save jacobp100/2db9e1aa39b9ffd1d396479ae13c056b to your computer and use it in GitHub Desktop.
// RENDER PAGE
server.all('*', (req, res) => {
const { store } = req;
match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
if (redirectLocation) {
res.redirect(301, redirectLocation.pathname + redirectLocation.search);
} else if (error) {
res.status(500).send(error.message);
} else if (!renderProps) {
res.status(404).send('Not found');
} else {
const { location, params } = renderProps;
const { dispatch, getState } = store;
const dataFetchingRequirements = renderProps.components
.map(component => component.WrappedComponent && component.WrappedComponent.fetchData)
.filter(fetchData => fetchData !== undefined)
.map(fetchData => fetchData({ location, params, dispatch }));
Promise.all(dataFetchingRequirements)
.catch(() => {}) // Ignore errors from data fetching
.then(() => {
const reduxState = getState();
const markup = renderToString(
<Provider store={store}>
<RouterContext {...renderProps} />
</Provider>
);
res.send(renderPage(markup, reduxState));
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment