Basically extending the documentation for educational purposes.
Universal app - main players on server side:
- react
- react-router - https://github.com/ReactTraining/react-router/blob/master/docs/guides/ServerRendering.md
- match to match the routes to a location without rendering
- RouterContext for synchronous rendering of route components
- react-redux - http://redux.js.org/docs/recipes/ServerRendering.html
Universal app - Why?
- SEO
- Performance (initial load time)
- Cross device support
- Same code base (sharing of logic)
- Catching current trends ;)
- Non js users (?)
In general, benefits for engineering, reusability, performance and SEO
Universal app - How?
- Handle the initial render when a user (or search engine crawler) first requests our app. When the server receives the request, it renders the required component(s) into an HTML string, and then sends it as a response to the client. From that point on, the client takes over rendering duties.
- Send the state of our app along in our response, so the client can use it as the initial state. if we preload any data before generating the HTML, we want the client to also have access to this data. Otherwise, the markup generated on the client won’t match the server markup, and the client would have to load the data again.
- Create a fresh, new Redux store instance on every request.On the client side, a new Redux store will be created and initialized with the state provided from the server.
- Optionally dispatch some actions.
- Pull the state out of store.
- Pass the state along to the client (dehydration - extract the current state of an app and serialize it into an object * ).
- Package & send the HTML to the client
Check server.js for detailed comments.
Universal rect explained
https://ifelse.io/2015/08/27/server-side-rendering-with-react-and-react-router/
https://www.codementor.io/reactjs/tutorial/redux-server-rendering-react-router-universal-web-app
https://github.com/reactjs/react-router/blob/master/docs/guides/ServerRendering.md
https://github.com/makeomatic/redux-connect/
Say I want to pre-render my app on the server, serve the html to the client, then re-render my app on the client. This would be trivial if my app only consisted of static data. However, my app is stateful: it retrieves data from my API before the initial render and stores it. By extracting the state of my app on the server, sending it along with the HTML, then reinjecting it on the client, I avoid making two requests to my API. for more see: http://stackoverflow.com/questions/29824908/what-does-dehydrate-and-rehydrate-stand-for-in-fluxible