Caching issues with global fetch being patched #48060
Replies: 1 comment
-
As you mentioned, I utilized the // Return a list of `params` to populate the [slug] dynamic segment
export async function generateStaticParams() {
const posts = await fetch('https://.../posts').then((res) => res.json());
return posts.map((post) => ({
slug: post.slug,
}));
}
// Multiple versions of this page will be statically generated
// using the `params` returned by `generateStaticParams`
export default function Page({ params }: { params: { slug: string } }) {
const { slug } = params;
// ...
} However, I discovered that the response obtained from the It did require some effort on my part to discover that this function caches data too and causing multiple pages to be built with outdated slugs. Now I'm aware of this behaviour, but it really cost me some time. I even initially suspected it to be a bug. I was thinking if you start a build process, all APIs are initially fetched again. I can totally understand why this could be a potential problem. |
Beta Was this translation helpful? Give feedback.
-
The current behavior of Next.js, which patches the global
fetch
method and caches it by default, is going to be causing issues for many developers who are not aware of this behavior. While it may seem like a convenient feature, it can lead to unexpected caching of API responses and other network requests, which can result in stale data being displayed to users.One major drawback of this approach is that it can make debugging more difficult, imagine fetching data from a CMS using a code snippet from their docs and the content is just cached by default. Developers may spend hours trying to troubleshoot a problem, only to find out that it was caused by the caching behavior of
fetch
in Next.js.I would love to know the reasoning behind this decision and also would propose naming it something else like
nextFetch
or something like that.Beta Was this translation helpful? Give feedback.
All reactions