Server-side Rendering ( SSR )
Server-side rendering (SSR) is one of the oldest methods of rendering web content. SSR generates the full HTML for the page content to be rendered in response to a user request. The content may include data from a datastore or external API.
The connect and fetch operations are handled on the server. HTML required to format the content is also generated on the server. Thus, with SSR we can avoid making additional round trips for data fetching and templating. As such, rendering code is not required on the client and the JavaScript corresponding to this need not be sent to the client.
With SSR every request is treated independently and will be processed as a new request by the server. Even if the output of two consecutive requests is not very different, the server will process and generate it from scratch. Since the server is common to multiple users, the processing capability is shared by all active users at a given time.
Pros and Cons
Executing the rendering code on the server and reducing JavaScript offers the following advantages:
Lesser JavaScript leads to faster
FCP
andTTI
: In scenarios where a page consists of multiple UI elements and application logic, Server-Side Rendering (SSR) involves significantly reduced JavaScript compared to Client-Side Rendering (CSR). Consequently, the script's loading and processing time is reduced. This results in shorter First Paint (FP), First Contentful Paint (FCP), and Time to Interactive (TTI). Additionally, with SSR, users experience an expedited display of screen elements and faster interactivity, eliminating prolonged waiting periods.SEO Optimized: Search engine crawlers are easily able to crawl the content of an SSR application thus ensuring higher search engine optimization on the page.
Slow Time to First Byte (TTFB): Since all processing takes place on the server, the response from the server may be delayed in case of one or more of the following scenarios:
- Multiple simultaneous users causing excess load on the server.
- Slow network
- Server code not optimized.
- Full page reloads required for some interactions: Since all code is not available on the client, frequent round trips to the server are required for all key operations causing full page reloads. This could increase the time between interactions as users are required to wait longer between operations. As a result, implementing a single-page application becomes challenging when using SSR.
Server-side Rendering in Next.js
The Next.js framework offers support for Server-Side Rendering (SSR). SSR involves pre-rendering a page on the server for every request. To achieve this, Next.js allows exporting an asynchronous function named getServerSideProps()
from a page, as illustrated below:
export async function getServerSideProps(context) { return { props: {}, // will be passed to the page component as props }}
The context object encompasses various keys that provide access to important elements such as the HTTP request and response objects, routing parameters, query strings, locale information, and more.
The subsequent example demonstrates how to utilize getServerSideProps() to render data:
// data fetched from an external data source using `getServerSideProps`
const Users = ({ users }) => { return ( <section> <header> <h1>List of Users</h1> </header> {users && ( <ul> {users.map((user, key) => ( <li key={key}> <td>{user.username}</td> </li> ))} </ul> )} </section> )}
export async function getServerSideProps() { // Fetch data from external API using jsonplaceholder const res = await fetch("https://jsonplaceholder.typicode.com/users") const data = await res.json()
// Pass data to the page via props return { props: { data } }}
export default Users