
- Next.js - Home
- Next.js - Overview
- Next.js - Project Setup
- Next.js - Folder Structure
- Next.js - App Router
- Next.js - Page Router
- Next.js Features
- Next.js - Pages
- Next.js - Data Fetching
- Next.js - ISR
- Next.js - Static File Serving
- Next.js - Pre-Rendering
- Next.js - Partial Pre Rendering
- Next.js - Server Side Rendering
- Next.js - Client Side Rendering
- Next.js Routing
- Next.js - Routing
- Next.js - Nested Routing
- Next.js - Dynamic Routing
- Next.js - Parallel Routing
- Next.js - Imperative Routing
- Next.js - Shallow Routing
- Next.js - Intercepting Routes
- Next.js - Redirecting Routes
- Next.js - Navigation and Linking
- Next.js Configuration
- Next.js - TypeScript
- Next.js - Environment Variables
- Next.js - File Conventions
- Next.js - ESLint
- Next.js API & Backend
- Next.js - API Routes
- Next.js - Dynamic API Routes
- Next.js - Route Handlers
- Next.js - API MiddleWares
- Next.js - Response Helpers
- Next.js API Reference
- Next.js - CLI Commands
- Next.js - Functions
- Next.js - Directives
- Next.js - Components
- Next.js - Image Component
- Next.js - Font Component
- Next.js - Head Component
- Next.js - Form Component
- Next.js - Link Component
- Next.js - Script Component
- Next.js Styling & SEO
- Next.js - CSS Support
- Next.js - Global CSS Support
- Next.js - Meta Data
- Next.js Advanced Topics
- Next.js - Error Handling
- Next.js - Server Actions
- Next.js - Fast Refresh
- Next.js - Internationalization
- Next.js - Authentication
- Next.js - Session Management
- Next.js - Authorization
- Next.js - Caching
- Next.js - Data Caching
- Next.js - Router Caching
- Next.js - Full Route Caching
- Next.js - Request Memoization
- Next.js Performance Optimization
- Next.js - Optimizations
- Next.js - Image Optimization
- Next.js - Lazy Loading
- Next.js - Font Optimization
- Next.js - Video Optimization
- Next.js - Script Optimization
- Next.js - Memory Optimization
- Next.js - Using OpenTelemetry
- Next.js - Package Bundling Optimization
- Next.js Testing
- Next.js - Testing
- Next.js - Testing with Jest
- Next.js - Testing with Cypress
- Next.js - Testing with Vitest
- Next.js - Testing with Playwright
- Next.js Debugging & Deployment
- Next.js - Debugging
- Next.js - Deployment
- Next.js Useful Resources
- Next.js - Interview Questions
- Next.js - Quick Guide
- Next.js - Useful Resources
- Next.js - Discussion
Next.js - use cache Directive
The "use cache" Directive
The "use cache" is a Next.js directive used to force the caching of a component or a function in application. You can define the directive at the top of any file to indicate that all exports in the file are cacheable, or inline at the top of a function or component to inform Next.js that the return value should be cached and reused for subsequent requests.
Example
This a simple example of "use cache" directive at the top of a component:
"use cache" // File starts from here import react from 'react' export default function Home() { return <div>Hello, Next.js!</div> }
The use cache directive is an experimental feature available only in canary channels and a subject to change.
Enable Support For "use cache"
As mentioned above, the "use cache" directive is an experimental feature, so you need to enable it in your project. To do that, you need set dynamicIO flag as true in your 'next.config.js' file.
// File: next.config.js import type { NextConfig } from 'next' const nextConfig: NextConfig = { experimental: { dynamicIO: true, }, } export default nextConfig
Caching Entire Routes
To prerender all the routes in your application, you can use the "use cache" directive at the top of layout component and page component. This is because the these components serves as the entry point for all the routes in your application.
'use cache' import { unstable_cacheLife as cacheLife } from 'next/cache' export default function Layout({ children }: { children: ReactNode }) { return <div>{children}</div> }
Output
The image below shows caching for the page we made in the previous chapter. Caches for all routes are stored and retrieved at user's browser.

Caching Function Output
You can use "use cache" directive inside a function to cache the return value of the function in Next.js. This is useful when you want to cache the network request, database query, or any other complex operation.
export async function getData() { 'use cache' const data = await fetch('/api/data') return data }
Output
The image below shows the output of the code above. You can see that the data is fetched from the server and cached in the browser.
