
- 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 - unstable_noStore() Function
The unstable_noStore() Function
The Next.js unstable_noStore()
function is used to opt out of static rendering and indicate a particular component should not be cached. This is useful for components that are not meant to be cached, such as a modal or a chat component.
Syntax
The syntax for the unstable_noStore()
function is as follows:
import { unstable_noStore as noStore } from 'next/cache'; export default async function ServerComponent() { noStore(); const result = await db.query(); }
Parameters
The unstable_noStore()
function accepts no parameters.
Return Value
The unstable_noStore()
function does not return any value.
Key Points
- The unstable_noStore() is a legacy API and no longer recommended. It's still supported for backward compatibility.
- Instead of using unstable_noStore(), you can use cache: 'no-store' on a fetch() call.
- The unstable_noStore is preferred over export const dynamic = 'force-dynamic' as it is more granular and can be used on a per-component basis
- Using unstable_noStore inside unstable_cache will not opt out of static generation. Instead, it will defer to the cache configuration to determine whether to cache the result or not.
Example 1
The code below, we defined a dynamic API route that fetches user data. We are using unstable_noStore() to opt out of static generation for this route. This means that the data will not be cached and will be fetched on every request.
// app/api/user/[id]/route.ts import { unstable_noStore } from 'next/cache'; import { fetchUserData } from '@/app/utils/db'; import { NextResponse } from 'next/server'; export async function GET( request: Request, { params }: { params: { id: string } } ) { // Opt out of caching for this route unstable_noStore(); try { const userData = await fetchUserData(params.id); return NextResponse.json(userData); } catch (error) { return NextResponse.json( { error: 'Failed to fetch user data' }, { status: 500 } ); } }
Example 2
The code below shows how to use unstable_noStore() to opt out of static generation for a specific component. This means that the component will not be cached and will be re-rendered on every request.
// app/components/Modal.tsx import { unstable_noStore } from 'next/cache'; export default function Modal() { // Opt out of static generation for this component unstable_noStore(); return <div>Modal</div> }