unstable_cache behavior with stale data is unclear #75462
-
SummarySo, as far as the documentation, what I have been told by people in the unofficial next.js discord, it would seem that when unstable_cache is queried and the cache is stale, it would provide you with stale data and then run the revalidation function in the background. However, even when running my application in production mode, this is not at all the behavior I see. When the Is there something I am doing wrong/misunderstanding/additionally needing to get this to work? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
Try ensuring your function includes: import { unstable_cache } from 'next/cache';
const fetchData = async () => {
const data = await fetch('https://api.example.com/data').then(res => res.json());
return data;
};
export const getCachedData = unstable_cache(fetchData, ['my-cache-key'], { revalidate: 60 }); Then in your component: const data = await getCachedData(); Run in production mode with logging: const fetchData = async () => {
console.log('Fetching fresh data...');
const data = await fetch('https://api.example.com/data').then(res => res.json());
return data;
};
export const getCachedData = unstable_cache(fetchData, ['my-cache-key'], { revalidate: 60 }); Then check if fresh data is fetched immediately or only after a delay. Workaround: Manually Implementing Stale-While-RevalidateIf const cachedData = cache.get('my-cache-key');
if (cachedData) {
// Serve stale data immediately
return cachedData;
}
// Fetch fresh data in the background
const freshData = await fetchData();
cache.set('my-cache-key', freshData, { revalidate: 60 });
return freshData;
|
Beta Was this translation helpful? Give feedback.
-
Hey, thanks for the detailed answer. So, the issue is I would like it so that it is never blocking except for on the first load. The first load, obviously it has to be blocking, but subsequent loads I would always like to display what is cached. If revalidation period is over, I do not want it blocked, I would like it to display what is in cache, then run the revalidation in the background. Is this possible? My processMapData is very computationally expensive, runs on another thread. So, it would very nice to have this functionality. Thanks. const getCachedMapData = (filePath: string) =>
unstable_cache(
async (filePath: string) => {
const mapData = await processMapData(filePath);
return {
compressedVolume : Array.from(mapData.compressedVolume),
dims : mapData.dims
};
},
[],
{ revalidate: 120, tags: [`voxel_${filePath}`] },
)(filePath);
export default async function GetMapData(filePath: string) {
const cachedMapData = await getCachedMapData(filePath);
return {
compressedVolume : new Uint8Array(cachedMapData.compressedVolume),
dims: cachedMapData.dims
};
} |
Beta Was this translation helpful? Give feedback.
-
Thanks, I will give something like that a shot. Good to know Cheers. |
Beta Was this translation helpful? Give feedback.
I see what you're asking now.. you want unstable_cache to always return cached data immediately, even after the revalidate period, and only update in the background.
Try this: