I'm running into an issue with the caching behavior of Next.js when using the { cache: 'no-store' } option in the fetch function. According to the Next.js documentation, this option should fetch data dynamically on every request and bypass the cache.
However, in my case, when I use { cache: 'no-store' }, it doesn't fetch the latest data from my Sanity CMS. Instead, it shows deleted posts and doesn't display newly created posts. This is the code I'm using:
const fetchPosts = async () => {
const query = `*[_type == "post"] | order(publishedAt desc){
title,
slug,
publishedAt,
mainImage {
asset->{
_id,
url
},
alt
},
author->{
name
}
}`;
try {
const posts = await client.fetch(query, { cache: 'no-store' });
console.log("Posts fetched:", posts);
return posts;
} catch (error) {
console.error("Sanity fetch error:", error);
return [];
}
};
On the other hand, when I use { next: { revalidate: 3600 } }, it fetches the latest data as expected.
I've tried restarting the development server, clearing caches, and inspecting the response headers. I've also tried useCdn = true and useCdn = false but the issue persists.
Can anyone explain why { cache: 'no-store' } isn't working as expected and showing me the latest data?