SSG and SSR in Next.JS
Server Site Rendering and Static Site Generation
Next.js 9.3 and newer offers special functions for data fetching: getStaticProps, getStaticPaths and getServerSideProps. Before Next.js 9.3, there was a function getInitialProps used for data fetching.
I tried finding answers to the following questions to really fully understand these special functions.
Is getStaticProps only good for rendering static pages like About or Blogs?
getStaticProps fetches data at build time when the code gets compiled into exe and serves to the client the rendered HTML. It is the fastest Static Site Generation, SSG. The contents are cached on CDN, which makes it even faster.
However, it does not mean that it only servers static pages.
getStaticProps can be used with
client-side fetching using libraries like
swr to get up-to-date information in run-time.
About page makes a request to
extreme-ip-lookup.com to find out user's city inside
This has the advantage of
- Super fast TTFP (time to first paint) where user can see the page loaded instantly
- JS request, interactions will be added soon after when they are loaded and executed. The SSG pages are fast and still capable of providing up-to-date information and interactivity.
When would you use getServerSideProps instead of getStaticProps
getServerSideProps fetches data at run time (when exe starts running) from
server. It is server-side rendering.
It is different from
getStaticProps in that:
- data is fetched at run time, not build time
- It provides better SEO as the data is rendered on server and available for crawler
- It is slower than SSG because HTML is generated at run time
- It can be faster than CSG because it is faster to make requests from server (but then you need to send the data back to client… hmm)
It is the same as
- Both can provide interactivity and up-to-date information (getStaticProps work well with client-side fetching!)
- Main pages or at least templates are rendered on the server
getServerSideProps is good when
- data is not static. Data needs to be fetched at run time
- SEO is important. We want the data to be pre-rendered
e.g. Medium paid content. Users need to be authorized as a paid member to view the articles so the article suggestions are to be generated in run time. (The article itself should be available at build time though.. or the content is put together at run time?)
How is getInitialProps different from getServerSideProps?
getInitialProps is an older method. It runs first on the server side and then it is run again on the client side evertime is it called. It is slower than
getServerSideProps because the code inside
getInitialProps is executed in the run time, whereas the NextJS just calls an API to run
getServerSideProps on the server, which then returns a
JSON data to the client.
- It is the only method that runs on both server and client
- initial data-population from server: good for SEO!
- It is the only method that runs on
In the discussion thread,
getInitialProps has this benefit that
- SSR to prefetch data on Server first for better SEO
- subsequently fetching the data using CSR
getServerSideProps request seems to block the transition during each transition on the client. See if this will go away in future.
Here is a long informative discussion thread on the various use cases and issues with NextJS SSR.
What is rehydration? How is it different from re-rendering?
React tries to reconcile the states during re-rendering whereas it just tries to mesh the views together in rehydration. Rehydration happens when we have some views rendered on the server side and then we fetch more data on the client side to display it.
See more in Josh’s Rehydration post