Photo by Kelly Sikkema on Unsplash
This is part 4 of my series on server-side rendering (SSR):
- π€·π»ββοΈ What is server-side rendering (SSR)?
- β¨ Creating A React App
- π¨ Architecting a privacy-aware render server
- [You are here] π Creating An Express Server
- π₯ Our first server-side render
- π Combining React Client and Render Server for SSR
- β‘οΈ Static Router, Static Assets, Serving A Server-side Rendered Site
- π§ Hydration and Server-side Rendering
- π¦ Debugging and fixing hydration issues
- π React Hydration Error Indicator
- π§πΎβπ¨ Render Gateway: A Multi-use Render Server
Over the previous three posts in this series we have described what server-side rendering (SSR) is, created a simple application using React, and discussed the architecture of a privacy-aware server to ensure we understand some of the sharp edges around SSR. Now, we will actually implement a basic server. Just as with the React application we created, the server we create will not be a complete solution, but it will provide a foundation from which we can continue to explore SSR.
β¨ A New Project
Where do we start? Well, we need a server that can receive web requests and respond to them. For that, I am going to use Express1, but first I need a project.
NOTE: Where you see yarn
, know that you can use your own package manager as you see fit.
- Add a new repository on GitHub (or your source control platform of choice).
- Make a new folder locally for your code
cd
to your new folder and rungit init
git remote add origin <your github repo URL>
git pull origin master
git branch --set-upstream-to=origin/master
- Create and commit a
.gitignore
file - Initialize it for JavaScript package management with
yarn init
- Run
yarn install
to generate the lock file - Commit the
yarn.lock
andpackage.json
to the git repository
Great, so now we have a project we can start working on. Let's add Express.
yarn add express
This should update our package.json
and yarn.lock
, so don't forget to commit those changes. I also recommend pushing often to your remote repository, that way your code is backed up online in case your computer suffers a nasty accident2.
ππ» Hello World!
At this point we need to write some code. We need to setup a route for our server that can handle providing a rendered result for any URL that our application might have. There are a couple of ways we could do this:
- Assuming that our server is invoked by some intermediate layer, such as a cache, we could have the server implement a single route (e.g.
/render
) and pass the URL to be rendered as a query parameter. - Our server could assume the URL is to be rendered by the client code and just accept any URL.
Option 1 gives us a great deal of flexibility in what our server can do, but it forces us to ensure that there is a layer between the original browser request and our server, as something has to be responsible for constructing the appropriate /render
route request. Option 2 removes the need to have an intermediate layer, but it perhaps restricts us from expanding server functionality. Of course, option 2 can be changed to option 1 if the need arises, so we can go with option 2 for now, knowing that later, it can be updated to suit changing needs.
Normally, I would add lots of other things to this server to improve development and runtime investigations, such as linters, testing, and logging, but for the sake of brevity, right now we will stick to the main functionality.
const express = require("express"); const port = 3000; const app = express(); app.get("/*", (req, res) => res.send("Hello World!")); app.listen(port, () => console.log(`Example app listening on port ${port}!`));
This is our index.js
file. It is not doing a lot. On line 4, we create our express app. On line 6, we tell it that for any route matching /*
, return Hello World!
. On line 8, we tell it to listen for requests on port 30003.
If we run this app with node index.js
, we can go to our browser, visit any route starting with localhost:3000
and see the text, Hello World!
. This is fantastic. We have a server and it is responding as we hope. Since we are going to run this often as we make changes, I will add a script to our package.json
to run node index.js
for us.
{ "name": "hello-react-world-ssr", "version": "0.0.1", "description": "A server-side rendering server", "main": "index.js", "license": "MIT", "dependencies": { "express": "^4.17.1" }, "scripts": { "start": "node index.js" } }
In the package.json
file shown above, I have highlighted the section I added containing the new start
command. From now on, we can start our app with yarn start
. The next step is getting our server to render our React application. Before we do that, consider these questions:
- How does the server know about and load the code for our React application?
- How does the server get the rendered result to send back?
- How do we isolate render requests to avoid side-effects bleeding across requests?
π€ The Hows
The answers to the first two questions have implications beyond the server itself, possibly influencing both our client application and any deployment process.
How our server knows about and loads our client application may affect how our server is deployed. Some server-side rendering solutions involve deploying the client-side code with the server so that it has direct access to the appropriate code, others use a mechanism such as looking up in a manifest to identify the files to load from a separate location (such as a content delivery network (CDN)). Neither of these is necessarily a bad choice – they both have their advantages and disadvantages. For example, deploying the server with the right code means:
- β The server has fast access to the client application it is rendering
- β The server can integrate nicely with the client application
- βThe render server must be deployed every time the client application changes
- βThe server is closely coupled to the client application
Whereas, looking up files in a manifest and loading them from elsewhere means:
- β The render server rarely requires updating
- β The server can render more than one application
- βThe server will probably need to cache JavaScript files locally or be at the mercy of latency when communicating with the CDN
- βThe client applications that the server renders likely need to include custom code to support being rendered by that server
Being aware of how these approaches differ – and they differ in more than just the ways I have suggested, is useful in understanding the trade-offs we must make when implementing our render server. Perhaps answering the second question will help us decided which route to take; consider how will our server get a rendered result of the client application?
Our server is going to invoke a call from the React framework that renders our React application to a string, rather than mounting it inside the DOM of a browser. To do that, it needs a React component to render, so it must load our client application and get the root-level component. In addition, assuming our render server is rendering the entire page and not just the React component, the server is likely going to need to gather additional information, such as which files must be loaded in the page, the page title, etc.
This whole process of capturing the application render and associated metadata requires interplay between the server code and the client code. Revisiting the first question and the two approaches I gave: if the server has the client code deployed with it, the server could know exactly which files to load to render the component, importing those directly and using them accordingly; if the server is less-closely coupled, we likely need some mechanism whereby the client application itself does more of the heavy lifting by hooking into some framework provided by the server, even if that is just exporting a specific object so that server can identify the appropriate things to coordinate rendering.
Ultimately, either we have a server that is custom built to our application, or we have a server that is built to support many applications. What to do? I say, dive in and try them both. To that end, next time we will look at the first option where the server knows all about the client application (though we may cut some corners to get to the salient points), and we will answer that third question; how do we isolate our renders?
ππ»ββοΈ In Conclusion
Herein we have created our server, though it does not do much yet. We have also considered two different approaches to connect our server to our client application: closely-coupled or more open, and we have started to think about how the server will isolate and respond to render requests.
This week's entry turned out a little longer than I had intended, and covered less things than I had hoped. Sometimes that is the way it goes. One of the biggest reasons I write these blogs is to discover what I do and do not know about something. Often in the effort of explaining it to someone else, I identify a bias that I have without any supporting evidence, or a topic I grasp that is far harder to explain than I expected.
Until next time, when we start to implement our server-side rendering, please leave a comment. Perhaps you have a question, a personal experience writing a render server, or want to take umbrage at something I have stated. I look forward to learning with you as we continue this journey into the land of SSR. πΊ
- I find Express easy enough to use and well-supported, though there are other options that one could use instead if one were so inclined [↩]
- A lesson from bitter experience; hard drives die (especially SSDs) without warning, drinks spill, laptops get dropped – keep your work backed up [↩]
- The port is currently hard-coded for simplicity, but we could make this configurable [↩]