Server-Side Rendering
Explore how to enhance React applications by rendering content on the server using Fastify and StaticRouter. Understand how to serve full HTML pages, manage dynamic routing, and handle status codes correctly to optimize performance and SEO. Gain practical skills in building universal JavaScript web applications with server-side rendering.
We'll cover the following...
We saw in the previous lesson that our application works and this is great news. However, the app is running only on the client side, which means that if we try to curl one of the pages, we’ll see something like this:
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>My library</title></head><body><div id="root"></div><script type="text/javascript" src="/main.js"></script></body></html>
No content whatsoever! There’s only an empty container (the root div), which is where our application is mounted at runtime.
Rendering the content
In this section, we’ll modify our application to be able to render the content from the server as well.
Let’s start by adding fastify and esm to our project by using the following command:
Now, we can create our server application in the src/server.js ...
There’s a lot of code here, so let’s discuss the main concepts ...