Skip to content

Latest commit

 

History

History
45 lines (27 loc) · 1.68 KB

51-Caching.md

File metadata and controls

45 lines (27 loc) · 1.68 KB

Caching Data in Next.js

Automatic Caching

Next.js, by default, automatically caches the returned values of fetch operations in the Data Cache.

Data Cache

What is data cache?

The data cache is a server-side cache that persists the result of data fetches across incoming server requests and deployments.

Why is it required?

The data cache improves app performance and reduces costs by eliminating the need to re-fetch data from your data source with every request.

Benefits of Data Caching

  1. Improved Performance: Faster response times for subsequent requests
  2. Reduced Server Load: Fewer calls to the data source
  3. Cost Efficiency: Potential reduction in API calls or database queries

Opting Out Of Caching

For individual data fetches, you can opt out of caching by setting the cache option to no-store.

Key points:

  • Once you specify the no-store option for a fetch request, subsequent fetch requests will also not be cached.
  • By default, Next.js will cache fetch() requests that occur before any dynamic functions (cookies(), headers(), searchParams) are used.
  • Next.js will not cache requests found after dynamic functions.

Example of Opting Out

const res = await fetch('https://...', { cache: 'no-store' });

Considerations

  • Use caching judiciously based on your data's nature and update frequency.
  • For frequently changing data or user-specific information, consider opting out of caching.
  • Balance between performance benefits and data freshness when deciding caching strategies.

By understanding and effectively using Next.js's caching mechanisms, you can optimize your application's performance and resource usage.