Last active
July 16, 2016 21:18
-
-
Save jacobp100/2db9e1aa39b9ffd1d396479ae13c056b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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