Adapted from this tutorial, this is a simple example for Server-Side Rendering using Next.js (through the API getServerSideProps
to render the quotes list on the server)
Based also on this great article, these are the main topics with SSR
Problem: Client-Side Rendering (CSR) - which is the default React behavior - means we need to download the JS, parse it, and then populate the DOM for the user to see something on the screen => large FCP time
- Server generates and sends populated HTML when requested by the client
- In doing so, it populates the data by querying APIs and DB => optimization of avoiding roundtrips between client and server
- SSR can reduce client-side processing, which would benefit slower devices
- With caching strategies and proper infrastructure support, server-side rendering can greatly enhance the overall user experience
Use cases:
- Highest gain if your application has a large amount of content or data that needs to be loaded before rendering the page
- For complex sites with a lot of dynamic content, there is a performance hit with SSR
- SSR can be more difficult to implement and maintain than CSR, adding to development costs.
- In many cases, a hybrid approach between SSR (for some components) and CSR (for other components) can be adopted
- Problem: CSR relies on JS, difficult to parse by search engines
- Solution: SSR delivers populated HTML which is parsed by search engines
- The
getServerSideProps
function is a special function that runs on the server before the page is rendered. It can be used to fetch data from an API or database, and return it as props to the page component. - Instead of fetching data in
useEffect
with [], fetch it insidegetServerSideProps
- Even better performance, server already has the HTML available from build time
- This HTML will then be reused on each request. It can be cached by a CDN.
- Next.js:
** Static Generation without data => Next.js generates a single HTML file per page during build time
** Static Generation with data (Your page content depends on external data): Use
getStaticProps
- Still in the Canary phase
- Components that run exclusively on the server (without using
getServerSideProps
) - Server Components never re-render. As far as React is concerned, this output is immutable, and will never change => we can't use state
- Has the potential to improve the Page Interactive (TTI) metric by reducing the additional JS code
Simple quote generator based on https://api.quotable.io/random
git clone https://github.com/3omdawy/next-ssr-quotes.git
cd next-ssr-quotes
npm install
npm run dev
Go to http://localhost:3000/quotes