Next.js offers various rendering modes that enable flexible development of web applications and cover different needs in terms of performance, SEO and user experience. The most important modes are server-side rendering (SSR), static page generation (SSG), and client-side rendering (CSR).
With SSR, the HTML content of a page is generated on the server side for each request. This improves SEO as search engine crawlers can see the fully rendered content immediately.
In Next.js, SSR is enabled by the getServerSideProps
function in the page components.
SSG generates static HTML pages at build time that can be reused with every request. This technique is ideal for pages whose content does not change frequently.
In Next.js, SSG is enabled by the getStaticProps
function (and optionally getStaticPaths
for dynamic
routes).
CSR means that rendering takes place entirely in the user’s browser. On the first request, only a minimal HTML framework is loaded and the content is generated by JavaScript in the browser.
CSR is used by default in Next.js in React components that do not contain SSR or SSG specific code.
Next.js also enables a hybrid mode where different rendering methods can be combined within an application. For example, an application could use SSG for the home page and SSR for individual product pages.
Develop a simple Next.js application that demonstrates the different rendering modes: server-side rendering (SSR), static page generation (SSG) and client-side rendering (CSR).
index.js
)./products
) that renders a list of
products on the server side.getServerSideProps
.