Next.js Functions: unstable_noStore
Last Updated :
13 Sep, 2024
In Next.js, the unstable_noStore function is a part of the experimental data fetching capabilities introduced to handle server-side rendering (SSR) and static generation more efficiently. As the name suggests, unstable_noStore is still experimental and subject to change, but it offers some unique advantages for certain use cases.
What is unstable_noStore?
unstable_noStore is a function used in Next.js to control the caching behaviour of server-side rendered pages. It allows developers to specify that a particular server-rendered page or API route should not be cached at all. This can be useful where data is highly dynamic and should not be served from the cache, ensuring that the most up-to-date information is always fetched directly from the source.
Syntax:
import { unstable_noStore as noStore } from 'next/cache';
export default async function Component() {
noStore();
const result = await db.query(...);
// Your component logic
}
Key Points:
- Preferred over dynamic = 'force-dynamic' for per-component control.
- Defers to the cache configuration when used inside unstable_cache.
When to Use unstable_noStore?
- Real-Time Data: When your page or API endpoint serves data that changes frequently, and you need to ensure that users always see the most current data without delay.
- Dynamic Content: For pages that display user-specific content or data that varies frequently, such as dashboards or live feeds.
- Development: During development, when you want to disable caching ensure that changes are immediately visible without needing to clear caches.
How to Use unstable_noStore?
Here’s a step-by-step guide on how to use unstable_noStore in Next.js:
Step 1: Import unstable_noStore
You need to import unstable_noStore from Next.js. This function is available in the next/experimental module.
import { unstable_noStore } from 'next/cache';
Step 2: Apply to Pages or API Routes
You can use unstable_noStore in two main contexts: server-side functions for pages and API routes.
For Server-Side Rendering (SSR) Pages: When fetching data on the server side for a page, you might use unstable_noStore in getServerSideProps to ensure that the page is not cached.
import { unstable_noStore } from 'next/cache';
export async function getServerSideProps(context) {
const data = await fetchDataFromAPI();
return {
props: {
data
},
unstable_noStore: true
};
}
const MyPage = ({ data }) => {
return (
<div>
<h1>Data:</h1>
<pre>{JSON.stringify(data, null, 2)}</pre>
</div>
);
};
export default MyPage;
For API Routes: Similarly, you can use unstable_noStore in API routes to prevent caching.
import { unstable_noStore } from 'next/cache';
export async function handler(req, res) {
const data = await fetchDataFromAPI();
res.setHeader('Cache-Control', 'no-store');
res.status(200).json(data);
}
export default handler;
Step 3: Testing and Debugging
Since unstable_noStore is experimental, it's important to thoroughly test your application to ensure that the caching behavior works as expected and that it doesn’t introduce unexpected issues or performance degradation.
Steps To Use Next.js Functions: unstable_noStore
Step 1: Create a New Next.js Application
Use the create-next-app command to bootstrap a new Next.js project:
npx create-next-app@latest my-next-app
cd my-next-app
Create Next.js applicationFolder Structure
Folder StructureDependencies
"dependencies": {
"react": "^18",
"react-dom": "^18",
"next": "14.2.11"
}
Example: Create a server component using unstable_noStore to opt out of caching
JavaScript
//src/app/page.js
import { unstable_noStore as noStore } from 'next/cache';
export default async function HomePage() {
noStore();
const result = await fetchDataFromDatabase();
return (
<main>
<h1>Next.js with unstable_noStore</h1>
<pre>{JSON.stringify(result, null, 2)}</pre>
</main>
);
}
async function fetchDataFromDatabase() {
return { message: 'Data fetched without caching' };
}
To strat the application run the following command.
npm run dev
Output
Next.js Functions: unstable_noStore
Similar Reads
Next.js Functions: notFound
Next.js is a React framework that is used to build full-stack web applications. It is used both for front-end and back-end development, simplifying React development with powerful features. In this article, we will explore the notFound function, which is used to handle cases where a requested resour
3 min read
Node.js sort() function
sort() is an array function from Node.js that is used to sort a given array. Syntax: array_name.sort() Parameter: This function does not take any parameter. Return type: The function returns the sorted array. The program below demonstrates the working of the function: Program 1: javascript function
1 min read
Underscore.js _.negate() Function
Underscore.js is a JavaScript library that provides a lot of useful functions that help in the programming in a big way like the map, filter, invoke, etc even without using any built-in objects. The _.negate() function is an inbuilt function in Underscore.js library of JavaScript which is used to fi
1 min read
Underscore.js _.noop() Function
Underscore.js is a JavaScript library that makes operations on arrays, string, objects much easier and handy. The _.noop() function is used to return "undefined" irrespective of the arguments passed to it. Note: It is very necessary to link the underscore CDN before going and using underscore functi
2 min read
Functions in Next JS
Next.js provides a suite of functions and hooks that enhance server-side rendering, static generation, routing, and more. These functions are essential for managing data fetching, handling requests, and optimizing performance in Next.js applications.Next.js FunctionsNext.js functions simplify server
4 min read
Underscore.js _.memoize() Function
The _.memoize() function is used to memorize a given function by caching the result computed by the function. It is used to speed up for the slow running process. Syntax: _.memoize(function, [hashFunction]) Parameters: This function accepts two parameters as mentioned above and described below: func
1 min read
Node.js util.types.isAsyncFunction() Method
The util.types.isAsyncFunction() method is an inbuilt application programming interface of the util module which is used to type check for asynchronous functions in the node.js. Syntax:Â util.types.isAsyncFunction( value ) Parameters: This method accepts a single parameter as mentioned above and des
2 min read
Underscore.js _.isNaN() Function
_.isNaN() function:Â It is used to find whether the value of the object passed is NaN or not.If the object's value is NaN then the output will be true otherwise, it will be false.We can even perform addition, subtraction etc operations in this function. Syntax:Â _.isNaN(object) Parameters:It takes o
3 min read
Underscore.js _.isNull() Function
Underscore.js _.isNull() function It is used to find whether the value of the object is null. If an object has a null value then the output will be true otherwise false. We can even perform addition, subtraction, etc operations in this function. Syntax:Â _.isNull(object);Parameters:It takes only one
3 min read
Underscore.js _.isError() Function
Underscore.js is a library in javascript that makes operations on arrays, string, objects much easier and handy. _.isError() function is used to check whether the given object is javascript Error Object or not. Note: It is very necessary to link the underscore CDN before going and using underscore f
2 min read