How to server-side render React, hydrate it on the client and combine client and server routes
Publish Date: Nov 4 '18
152 10
How to server-side render React, hydrate it on the client and combine client and server routes
In this article, I would like to share an easy way to server-side render
your React application and also hydrate your Javascript bundle on the
client-side. If you don't know what "hydrate" is, I'll try to explain: imagine
that you render your React component to a string using the ReactDOMServer API,
you will send HTML to the client, that is static. In order to deal with the
dynamic events you've set in your component, you will have to attach this HTML
markup to its original React component. React does so by sending an identification
to the generated markup so it is able to resolve later which event should be
attached to which element in the DOM. (Kind of). You can read more at the
official docs.
In my previous attempts to properly render my app on the server and hydrate it
on the client, I've got lost in the Webpack configuration: it has been
changing quite a bit in any major release, so often documentation and tutorials are obsolete. This is also my attempt to try to save you some time.
I tried to keep it as verbose as possible to ease the learning process, so I've divided it into seven parts:
Initial Webpack configuration
First server-side rendering
Switch to Streams
Combine the Express router with React Router
Using Express query string
Create a test environment
(Try to) code split
Initial Webpack configuration
First we should install our dependencies:
npm i -E express react react-dom
and our development dependencies:
npm i -DE webpack webpack-cli webpack-node-externals @babel/core babel-loader @babel/preset-env @babel/preset-react
other tools that will helps us in development:
npm i -DE concurrently nodemon
Let's configure Webpack. We will need two Webpack configurations, one for the
Node.js server code and another one for the client code. If you want to see the structure of our app, please
refer to the repository. Also, please note that:
I'm using the ES2015 preset
instead of the new env preset, you can change it on your own if you want to.
I've also included the
transform-class-properties
Babel plugin so I don't need to .bind my classes methods everywhere. It's up to you if you want it, but it's on CRA by default.
Since I'm using the same module rules for both server and client I will extract
them to a variable js:
Note that in both configurations I'm using different targets.
On the server configuration, there are two details I've missed in my previous attempts to do server-side rendering and by doing so I was not able to even build my app: The node.__dirname property and the use
of the Webpack plugin webpack-node-externals.
In the first case I've set __dirname to false so when Webpack compile our server code it will not provide a polyfill and will keep the original value of __dirname, this configuration is useful when we serve static assets with
Express, if we don't set it to false Express will not be able to find the
reference for __dirname.
The webpack-node-externals is used so Webpack will ignore the content of node_modules,
otherwise, it will include the whole directory in the final bundle. (I'm not
sure why it's not the default behavior and we need an external library for this.
My understanding is that if you have set your configuration target to
node, it should have kept the node_modules out of the bundle.)
Note: In both cases, I found the documentation really confusing so please don't take my word for it and check the docs yourself in case of further questions.
Here we are building and then concurrently watching for
changes in our bundle and running our server from /dist. If we start our app without the
first build, the command will crash since there is no files in /dist yet.
If you npm run dev in your terminal your app should be available at localhost:3000.
Switch to Streams
Now we will switch to the stream API in order to improve our performance, if you
don't know what streams are about you can read more about them here and
more specific to React here.
Then let's create two components as we did for Home. The first one will be almost the
same as the basic example in the React Router
docs, let's call it MultipleRoutes:
in our server we will import the new component and also the React Router
library. We will also create a wildcard route /with-react-router*, so every
request to /with-react-router will be handled here. E.g.: /with-react-router/one, /with-react-router/two, /with-react-router/three.
Note that we have used different routers from react-router-dom in the
client and the server.
By now you must have an app that have both client and server rendered routes. To
improve the navigation we will add a link to /with-react-router in our Hello component:
As we have set a full Node.js application with Express we have access to all the
things that Node has to offer. To show this we will receive the prop name of
the Hello component by a query string in our / route:
Here we are defining a default value for the variable name if req.query does
not provide us one. So, the Hello component will render any value you pass
for name at localhost:3000?name=anything-I-want-here
Create a test environment
In order to test our React components we will first install a few dependecies. I've chosen Mocha and Chai to run and assert our tests, but you could use any
other test runner/assert library. The down side of testing this environment is
that we have to compile the tests files too (I'm not sure if there's any other
way around it, I think not).
npm i -DE mocha chai react-addons-test-utils enzyme enzyme-adapter-react-16
So I'll create a new Webpack config for tests, you'll note that the configuration is almost
exactly the same as we already have for the server files:
At this point, running npm test should pass one test case.
(Try to) code split
Well I honestly think that the new way to do code splitting with Webpack is a
little bit
difficult to understand, but I'll try anyway. Keep in mind that this is
not a final solution and you'll likely want to tweak with Webpack to extract the
best from it, but I'm not willing to go through the docs now for this. The
result I've got here is good enough for me. Sorry. Head to the docs in
case of questions.
to our clientConfig, Webpack will split our code into four files:
home.js
multipleRoutes.js
vendors~home.js~multipleRoutes.js
vendors~multipleRoutes.js
it even gives us a nice report when we run npm run dev. I think these files are
quite self-explanatory but still, we have files that are exclusive for a given
page and some files with common vendor code that are meant to be shared between
pages. So our script tags in the bottom of the / route would be:
Question: Why are you setting up redux both server side and client side? Should the server and client redux stores be instantiated with the same reducer? What ever happened to Don't Repeat Yourself (DRY)? Aren't we passing the initial state to the front end anyway (so then we're creating a store on the front end with that)?
I am asking with pure curiosity... Learning SSR right now and I've found this demo to be extremely helpful. Just confused on this.
Would also love a little more clarification why there is a react-router-dom router set up on the front and back end, but that can be answered separately.
Hey, bro! What's up? I'm actually not using Redux here. But initiate it on the server could be useful in case you want to dispatch some stuff on the server first. :)
I'm glad you found it useful!
The react-router is set up both on the server and client because you need a root provider for your React app in both cases. Cheers!
hi here, im late sorry :)
why do you need react-router for client and server, if i cancel the client webpack config, and i'm using only the server webpack config it's working, but there is no code-splitting , and i don't understand why i can't display the tutorial anymore. I thought, 'src/index' was sufficient for all application's requests, what did i miss ?
It's okay, np. I think your question is related to the 'hydration' of the app so I will share a link to the official docs where they do a great job explaining it. OK? Other than that it might be helpful to read the first paragraph of the tutorial again. Please don't be discouraged if you don't quite grasp it at first, it is a non trivial subject.
I'm sorry I didn't see your message before. I'm don't know whether you are familiar with the concepts of Node.js streams. So I will share some links that will hopefully help you. But in summary this method returns a Node.js Readable Stream. This has performance gains over i.e. renderToString since it doesn't wait to parse your React nodes into a string, instead it will sending you data as it is parsing. If you don't have a huge component I would advise you to use renderToString instead since it is much simpler.
Regarding the state while streaming, let me know what's your specific question. Cheers.
Thanks, this tutorial and code snippets helped a lot! All the other things I looked at where too complex and had extra code. I just needed to know how to combine string rendering and hydration.
A built an app based around your instructions. I hope it helps others here too.
We set up a client-side React app with some components including an incrementing counter.
On initial page load without JS running, a user or a search engine crawler will see an empty page. So we add a server-side Express app that return an HTML page which acts as a fully-rendered starting point that needs no JS to view in the browser.
We do this by calling ReactDOMServer.renderToString, which unfortunately freezes the app so that ignores user interaction. This is solved by calling React.hydrate on the client, so that the browser can make the initial HTML and turn it into a dynamic app in the usual SPA style.
Question: Why are you setting up redux both server side and client side? Should the server and client redux stores be instantiated with the same reducer? What ever happened to Don't Repeat Yourself (DRY)? Aren't we passing the initial state to the front end anyway (so then we're creating a store on the front end with that)?
I am asking with pure curiosity... Learning SSR right now and I've found this demo to be extremely helpful. Just confused on this.
Would also love a little more clarification why there is a react-router-dom router set up on the front and back end, but that can be answered separately.