![]() |
Caching is all about storing copies of the data in temporary storage, and that stored data is called cache. The main reason to do caching is that, in the future, when that data is needed, it can be served faster, and we don’t have to fetch the data from the original source, because that can take some extra time, which can cause a performance issue, if the fetching process is done quite frequently. Caching in Next.jsNext.js, a popular React framework, offers several caching strategies that developers can use to optimize their applications. Steps to create the NextJS ApplicationStep 1: Create a React application using the following command: npx create-next-app@latest testapp Step 2: After creating your project folder i.e. testapp, move to it using the following command: cd testapp Step 3: After creating the nextJS app we have to create an another directory testpage inside that create a file page.tsx, after all that this is what our project directory will look like. Project Structure![]() Folder Structure Dependencies"dependencies": Static Generation (SSG)Static Generation is a method where HTML pages are pre-rendered at build time. This results in faster load times since the content is served as static files. Pages generated this way can also be cached on the CDN level. Incremental Static Regeneration (ISR) allows you to update static content after the site has been built and deployed. With ISR, you can regenerate specific pages based on a predefined interval or trigger, ensuring the content remains fresh without rebuilding the entire site. Example: By using the export async function getStaticProps() { In this example, the page will be revalidated and regenerated every 10 seconds, ensuring fresh content while maintaining the performance benefits of static generation. Server-Side Rendering (SSR)Server-Side Rendering generates HTML pages on each request. While SSR ensures that content is always up-to-date, it can be slower compared to SSG. To improve performance, caching strategies can be implemented to store rendered HTML and reuse it for subsequent requests. Example: Using HTTP headers, you can instruct the browser to cache server-rendered pages. Here, Cache-Control header specifies that the response can be cached by any cache (public), should be revalidated after 10 seconds (s-maxage=10), and can be served stale while revalidating for up to 59 seconds. export async function getServerSideProps(context) { API CachingAPI routes in Next.js can benefit from caching to reduce latency and server load. Implementing caching in API routes ensures that frequently accessed data is served quickly. Example: Using a caching library, you can cache API responses in Next.js. import { NextApiRequest, NextApiResponse } from 'next'; Client-Side CachingClient-side caching involves storing data in the browser to reduce the need for repeated network requests. This can be achieved using service workers, local storage, or in-memory caching. Example: Using the react-query library, you can cache API responses in memory. import { useQuery } from 'react-query'; Best Practices for Caching in Next.js
Example : In Next.js, server-side rendered pages are automatically cached, retaining state between navigations, while client-side rendered pages do not inherently cache state, resulting in potential data changes with each navigation.
Output |
Reffered: https://www.geeksforgeeks.org
ReactJS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |