Implementing server-side rendering
Now that we know why server-side rendering is useful, let's implement it in our application. First, we will need to think about what we need to do to implement this functionality:
- Figure out which (sub)page was requested.
- Gather all the data required to render the page.
- Render the HTML using this data.
- Send this HTML to the client.
For this section, we will use the template code from chapter10_1.zip
. This template adds a file on the server where we can implement server-side rendering without having to deal with the rest of the server code.
Handling the request/routing
In order to be able to render any page on the server, we need to make use of react-router on the server. First, we will emulate a Redux store, browser history, and location. Then, we can make use of the match
helper function of react-router
to decide which page to display.
Emulating the Redux store and browser history
First, we need to create a Redux store and history object (for the router):
- Open
server...