Last active
October 4, 2018 06:09
-
-
Save limelights/c7ef93efd4abba5a7ea1 to your computer and use it in GitHub Desktop.
Basic SSR for express app
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
| // Server | |
| app.use((req, res, next) => { | |
| match({ routes, location: req.url }, (error, redirectLocation, renderProps) => { | |
| let store = setupStore(); | |
| let promises = collectPromises(req.url, renderProps.routes, store); | |
| collectToken(req.cookies, store); | |
| Keys.all(promises).then(() => { | |
| const html = renderToString(<Provider store={store}> | |
| <RoutingContext {...renderProps} /> | |
| </Provider>); | |
| res.status(200).send(template.render({content: html, data: JSON.stringify(store.getState())})); | |
| }).catch((err) => { | |
| next(err); | |
| }); | |
| }); | |
| }); | |
| // Client | |
| document.addEventListener('DOMContentLoaded', () => { | |
| const state = window.__BOOTSTRAP__ || {}; | |
| const store = setupStore(state); | |
| render(<Provider store={store}> | |
| {createRoutes(createBrowserHistory())} | |
| </Provider>, | |
| document.querySelector('#app')); | |
| }); | |
| // A Random Component | |
| class RandomComponent extends Component { | |
| static fetchData () { | |
| return { | |
| getSomeAsyncData | |
| }; | |
| } | |
| render() {} | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment