Is fetch supposed to cache in between next build
commands?
#83639
Replies: 2 comments
-
Thanks for this - I'll put this in my stack of TODOs. Here are some thoughts on this matter...
Right, this is problematic, because the next run, doesn't clean the cache (though
In this case the statement
Not that it is way better either, there's observable behavior here that we should communicate. I'll see what I can do to surface this.
Also agree here, we need to improve this bit. When no-store is used, fetch is taken explicitly as a dynamic API, nearly equivalent to saying When you override with This is a bit tricky to document I think - since it is something that Please do follow up if you think I've missed the mark - gotta get the facts right. |
Beta Was this translation helpful? Give feedback.
-
It looks like you’ve nailed down the tricky behavior of Next.js fetch and caching during builds. Since you’re already working with Pokémon detail pages, your site Pokémon Team fits perfectly it’s built to help fans plan balanced teams, analyze type coverage, and enjoy strategy beyond just fetching Pokémon data. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
I'm trying to statically prerender HTML files for one of my back-end resources, in the form of pokemon detail pages.
I'm running on
next
version15.5.2
and I'm seeing the following behaviour: when I runnext build
it seems to cache my requests in between runs onnext build
. As I thought that in Next.js 15 that fetches are no longer cached by default, the documentation feels a little unclear.Here is my journey, first this is the code for a pokemon detail component I started with:
Note: I'm using the
generateStaticParams
so at build time the pokemon detail pages can be statically generated.When I'm running the command
next build
for the very first time, or when deleting the.next/cache
directory my back-end is hit with the following request at build time:The output for
next build
shows:Plus the
.next/server/app/pokedex
folder is filled with generated HTML files as you would expect. So all is good.However if I run
next build
again the back-end is never hit it seems that the requests are cached between builds.The documentation of fetch states (bold emphasis me):
"auto no cache (default): Next.js fetches the resource from the remote server on every request in development, but will fetch once during next build because the route will be statically prerendered. If Dynamic APIs are detected on the route, Next.js will fetch the resource on every request."
Either the documentation is inaccurate as it should also state that
no cache
does cache requests betweennext build
commands, or this is a bug of sorts.So then I tried setting my cache option to
no-store
to force Next to hit my back-end:However when I run
next build
no HTML files are generated in.next/server/app/pokedex
and my back-end is not hit. The output of the build command shows something interesting:The 'rendering' logs are no where to be found, it seems Next.js decides that it should not render anything. When I start the server I can see that it fetches the pokemon on demand.
After some digging I hit on const dynamic on the generateStaticParams docs.
So when I set `export const dynamic = 'force-static' all HTML files are generated and my back-end API is always hit.
In short: I feel that the documentation should make the interactions between
no cache
andgenerateStaticParams()
more clear, as well as the link betweenforce-static
andno-store
, on the cache options documentation.Beta Was this translation helpful? Give feedback.
All reactions