November 3, 2024

Md. Saad

Caching is the process of storing copies of files in a cache or temporary storage location so that they can be accessed more quickly. Technically, a cache is any temporary storage location for copies of files or data, but the term is often used in Internet technologies.
In recent updates, Next.js has brought significant changes in performance optimisation, and caching plays a pivotal role in enhancing user experience, lowering latency, and reducing server load. In this blog, we’ll explore how caching works in Next.js, covering its different layers, caching strategies, and best practices for making your Next.js application blazing fast.
By default, Next.js maximises caching to enhance performance and reduce costs. This results in routes being statically rendered and data requests being cached unless explicitly disabled. The diagram below illustrates the default caching behaviour, including when a route is statically generated during build time and when a static route is accessed for the first time.
Caching mechanisms work in four ways. These are request memorization, data Data Cache, full route cache, and router cache. Here’s a detailed breakdown of the caching mechanisms:
React automatically memoizes fetch requests with the same URL and options. This means you can call a fetch function multiple times across a component tree without triggering multiple network requests.
Features
Deduplicated Fetch Requests
For example, if you need the same data in multiple components, you don't have to fetch it at the top level and pass it down. Instead, you can fetch data directly within each component, and React ensures the request is made only once:
async function getItem() {
const res = await fetch('https://.../item/1');
return res.json();
}
const item = await getItem(); // First call: cache MISS
const item = await getItem(); // Second call: cache HITOpting Out:
Memoization can't be opted out of for GET requests, but for managing individual requests, you can use the signal property from AbortController to abort in-flight requests.
Next.js has a built-in Data Cache that persists data fetched via the fetch API across server requests and deployments. This optimises performance by allowing server-side fetch requests to interact with a persistent cache.
Features
Note: Data Cache is available in pages/routes but not in middleware. If deployed on Vercel, consult Vercel Data Cache documentation for platform-specific features.
Next.js automatically caches routes at build time for faster page loads. This process, often called Automatic Static Optimization or Static Rendering, caches rendered routes on the server, enabling cached responses instead of server-side rendering for each request.
Features
The Full Route Cache persists across requests but is cleared on new deployments.
The Router Cache (also called Client-side or Prefetch Cache) is an in-memory cache in the browser that stores the React Server Component Payload for individual route segments during a user session. It improves navigation by caching visited routes and prefetching potential future routes.
Features
You can adjust invalidation times using the experimental staleTimes option.
Caching in Next.js is a powerful tool for optimizing your web applications. Whether you’re building static sites, dynamic SSR pages, or using APIs, effective caching strategies can significantly improve performance, reduce server load, and enhance the overall user experience. With new caching capabilities and optimizations in Next.js, it's easier than ever to ensure your application is fast, scalable, and ready for the modern web. Thus, By understanding and applying the caching strategies discussed here, you can take full advantage of Next.js performance features and build highly performant, user-friendly applications.
I hope you got a lot out of reading this. Please do not hesitate to contact StaticMania with any questions or feedback. Happy coding!