Next.js memory cache is an essential tool for optimizing page loads and improving user experience. By caching frequently accessed pages, you can reduce the number of requests to your server and speed up page loads.
To get started with Next.js memory cache, you need to enable it in your next.config.js file. According to the documentation, you can do this by setting the cacheManager option to true.
Enabling memory cache can significantly reduce the number of requests to your server. For example, if you have a page that is accessed 100 times per hour, enabling memory cache can reduce the number of requests to 1-2 times per hour.
Caching in Next.js
Next.js has a built-in caching strategy that caches fetch requests by default, so you'll see the same value repeatedly if you deploy your app to Vercel and hit your production page a few times.
This caching strategy can be overridden by passing a caching option to your fetch requests or to the page. To invalidate a fetch every 10 seconds, you can pass { next: { revalidate: 10 } } to your fetch, which is an extension to the standard fetch API from Node.js.
Next.js has several caching options available, and you can learn more about them by reading Next.js's documentation page on caching.
Next.js Features
Next.js provides built-in support for server-side rendering (SSR) and static site generation (SSG), allowing for faster page loads and improved SEO.
The framework also includes features like internationalization and localization, making it easy to create multilingual websites.
Automatic code splitting is another key feature, which helps reduce the amount of code that needs to be loaded on each page load.
This results in faster page loads and a better user experience.
Next.js Server Components
Next.js Server Components are a game-changer for performance and efficiency.
By using Server Components, Next.js overrides all fetch requests and adds caching to them, which means you'll see the same value repeatedly on a production page if you don't pass any caching options.
Next.js has a default caching strategy that caches results when building the page, unless you specify otherwise.
To tell Next.js to invalidate a fetch every 10 seconds, you can pass { next: { revalidate: 10 } } to your fetch request.
This is an extension to the standard fetch API from Node.js, and it's a powerful tool for controlling caching behavior.
Next.js's caching options are extensive, and you can learn more about them by reading the documentation page.
Apollo
Apollo can patch fetch requests, which means it's affected by Next.js caching.
Next.js caches all fetch requests, regardless of whether they're GET or POST, so Apollo requests are cached too.
To avoid caching on the server side, you can update your client accordingly.
Deploying to Vercel and hitting the page will show the same behavior as a plain fetch request.
Usage and Implementation
Next.js memory cache is a powerful tool for improving performance, and implementing it is relatively straightforward. To get started, you'll need to install the `next-cache` package using npm or yarn.
The memory cache can be enabled by adding the `cache` option to your `next.config.js` file. This is as simple as adding a single line of code: `module.exports = { cache: true }`.
The cache can be configured to store data in memory for a specific amount of time, known as the TTL (time to live). This means that if a page is accessed again within the TTL period, the cached version will be served instead of re-running the code.
By default, the TTL is set to 1 minute, but you can adjust this value to suit your needs. For example, if you set the TTL to 5 minutes, the cached version of a page will be stored in memory for 5 minutes before being removed.
Sources
- https://nextjs.org/docs/app/building-your-application/data-fetching/fetching
- https://nextjs.org/docs/app/api-reference/directives/use-cache
- https://www.apollographql.com/blog/how-to-use-apollo-client-with-next-js-13
- https://nextjs.org/docs/app/building-your-application/optimizing/memory-usage
- https://nextjs.org/docs/app/building-your-application/caching
Featured Images: pexels.com