Pre-rendering is a key feature of Next.js that refers to the generation of HTML for each page in advance, i.e. before it is sent to the client. Unlike many traditional JavaScript frameworks that use purely client-side rendering (CSR), Next.js by default generates each page server-side or statically as HTML before it is delivered to the browser.
Next.js supports two main forms of pre-rendering: Static Site Generation (SSG) and Server-Side Rendering (SSR).
With SSG, the HTML content of the page is generated at build time and can be reused for every request. This method is ideal for pages whose content does not change frequently.
getStaticProps
and
getStaticPaths
(for dynamic routes).With SSR, the HTML content is regenerated for each individual request. This is useful for pages with dynamic content that changes frequently.
getServerSideProps
.2 Better SEO: Because the HTML content is available upfront, search engines can index the pages more easily.
3 Stability and reliability: Statically generated pages are independent of server requests and therefore more reliable.
Next.js also allows a hybrid approach where SSG and SSR can be combined within the same application depending on the requirements of each page.
In addition to pre-rendering, dynamic content can also be loaded and
rendered on the client side in Next.js to create interactive user
experiences. This is mostly done through the use of React hooks such as
useState
and useEffect
.
Develop a simple blog application with Next.js that offers the following features: 1. Static Page Generation (SSG) for blog posts. 2. Server Side Rendering (SSR) for a dynamic “About Us” page. 3. Client-side rendering (CSR) for comments on each blog post.