Next.js Server-Side Rendering Practical Example
On this issue, we will be talking about Next.js Server-Side Rendering, enabling high-performant websites by pre-rendering HTML.
Hello, my name is Igor and if by chance, you enjoy what I write, you may also like what I have to say on my Twitter profile, come and say 'Hi! š'.
This article is part of an entire Next.js series of articles I am putting together to help you become a Next.js pro and build blazing-fast React apps.
š” If you donāt want to miss out on any of the tutorials, signup for my newsletter by clicking here or head over to DailyDev.io for more.
On this issue, we will be talking about Next.js Server-Side Rendering, complementing the last video on the pre-rendering topic.
So letās get started!
Be sure to check out my previous articles on Next.js pages, Dynamic Routing, and Static Site Generation before continuing to better understand what we will be doing here.
Quick Recap
The last time we were here, we introduced the concept of pre-rendering, the default method that Next.js uses to render its pages, resulting in better performance and less effort for the client, by serving the client with a pre-built HTML file bundled with the necessary JavaScript file to make it work.
We discussed that Next.js presented two main ways of pre-rendering: Static Site Generation and Server-Side Rendering. That time, we covered the former and updated our upcoming dog-searching app to reap the benefits of that Next.js feature, and on this one, we will be learning something new.
Server-Side Rendering
Server-Side Rendering works on the same basis as Static Site Generation where the goal is to serve the client with a pre-built static HTML page that the server has pre-rendered and that is then hydrated by the bundled JavaScript file, restoring the page to its true interactive glory.
The main difference to retain between these two pre-rendering methods that Next.js ships with, is when the pageās HTML is pre-rendered.
Static Site-generated pages are pre-rendered at build time when the next build
command is run. It is a case of generating and forgetting about them until a new build is necessary to reflect new changes.
On Server-Side-generated pages, however, the pageās HTML is generated on each request to the server. This means that the same page is rendered multiple times along the applicationās lifetime. As many as it is requested (unless we use caching, more on that later).
As you can see, server-side rendered pages will be less performant than statically generated ones and cannot be stored on a CDN for faster global access.
However, sometimes Server-Side Rendering may be the only option. The main reason you might be looking into using Server-Side Rendering over Static Site Generation when creating a page is if that page needs to pre-render frequently updated data that must be fetched from some external data source and canāt, therefore, be statically generated at build time as it needs to always be up-to-date.
How to Use It
Unless we specify that a page is to be either server-side rendered or statically rendered with data (check the previous article), Next.js wonāt be able to tell the difference.
To do this, Next.js allows us to define specific methods within our pageās components that it will then evaluate and assess whether each page is to be pre-rendered at build time or on each request.
Previously, we discussed the Static Site Generation-specific methods and Server-Side Rendering is no different.
To use Server-Side Rendering for a page, we export an async
function named getServerSideProps
from our pageās React Component. This is the function that the Next.js server will call on every request to this page.
This function will feed any data that we wish into our page component through its props
, by having a very specific return type.
And just like that, our page became server-side rendered and will always be up-to-date every time it is requested! š
Updating our Dog App
Now letās create a server-side rendered page within our Doggo App to see it in action.
For the purpose of this tutorial, we will create a page that will display a dog carousel whose data will be coming from the server every time we request that page.
Creating our New Page
Letās create a new file under the /pages/dogs
directory called carousel.tsx
.
Export a new React Component called Carousel
that will receive a data
object as component props
that are typed with an array
of our Dog interface
created in the last article (check /definitions/dogs.d.ts
).
Render some HTML in it and your component should look something like this:
Making it Server-Side Rendered
Now letās add the magical function that makes this a server-side rendered page, the getServerSideProps
function.
This function will fetch all dogs in our in-memory database that we created as a TypeScript file exporting an array
(check /db/dogs.ts
, we also added a few more entries for this tutorial), and return them as a randomly sorted list so that our newly created component can use it to create a āfrequentlyā updated carousel.
Our function should be something similar to this:
Rendering the Data
Now that we have our component receiving data, we can take it and do whatever we please with it.
Iāll be creating a two-column grid
and inside it, we go through each item on our data
prop (that we know is a Dog
) and we render their name and description on the screen. Of course, we also add some CSS just so that this doesnāt become a hot mess of HTML. š
Maybe something like this:
Final Result
So by the end, the page that can be found under /dogs/carousel
should look something like this and be updated each time we request it with data fetched by the server. Cool! š
Final Remarks
To experience the differences between Static Site Generation and Server-Side Rendering, donāt forget to run the build command (npm run build
) followed by serving (npm run start
) the generated files. This is how the pages would be served in a production environment and cached by some CDN.
Running the project in npm run dev
mode will always build all pages on each request, running all specific pre-rendering functions on all requests.
Notice how Next.js detected which pages were static or server-side rendered.
If you run into any trouble feel free to reach out to me on Twitter, my DMs are always open.
Next Steps: Keep an eye out for my following Next.js tutorials where we will go over much more in Next.js territory! If you donāt want to miss out on any of the tutorials, signup for my newsletter by clicking here.