Open mastoj opened 5 hours ago
Typo. “ For some reason the fetch call that is wrapped using a higher order function want cache at all and we get the following output:”.
won’t cache**
Force dynamic is used to ensure that the request is generated at request time. It is not used for caching.
https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config#dynamic
As for using revalidate in the fetch request. You are not using the ‘cache’ attribute as well. Which in NextJS 15 defaults to no store.
https://nextjs.org/docs/app/api-reference/functions/fetch
So you will need to set that if you want the fetch request to be cached.
Note the docs say:
As a convenience, it is not necessary to set the cache option if revalidate is set to a number.
But I believe this is only true for NextJS 14. Please try setting ‘“cache”: “force-cache”,’ and try again if you are using NextJS 15.
You can read more about this change here.
https://nextjs.org/blog/next-15 https://nextjs.org/blog/next-15-rc
(fixed typo)
Added cache: "force-cache"
with no difference in behavior: https://github.com/mastoj/wrapped-fetch-debug/commit/dff943b2babfda5313874217f9c8cdbc9964cc2b
Link to the code that reproduces this issue
https://github.com/mastoj/wrapped-fetch-debug
To Reproduce
I have the exact same app in nextjs 14: https://github.com/mastoj/wrapped-fetch-bug-14 and nextjs 15: https://github.com/mastoj/wrapped-fetch-debug
What we are doing is testing what is happening when you wrap fetch in a higher order function, since that is a natural way to componse functions together.
Both of the solution is not behaving as I expect them to. To reproduce the issue in any of the repos do:
pnpm run build
pnpm run start
Current vs. Expected behavior
Doing the first load will print the logs from the api/route:
which is as expected. Doing the second load of the page is what makes things interesting. In our
/debug
page we are making three api-calls in a slightly different way, but I expect them all to be cached.Nextjs 14 behavior
For some reason the fetch call that is wrapped using a higher order function won't cache at all and we get the following output:
Nextjs 15 behavior
In 15 the behavior differed quite a lot from 14. To some degree it make sense since the defaults has changed. Since we are using explicit
const dynamic
andrevalidate
on fetch I would actually expect the behavior to be the same, but that is not the case. In 15 it seems like nothing is cached and we make api calls every time.Expected behavior
Even though we have
export const dynamic = "force-dynamic"
I would expect therevalidate
on the fetch calls to be honored. That assumptions means I would expect no outputs from the log from the routes to be outputted since no calls should be made.Provide environment information
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next start (local), Vercel (Deployed)
Additional context
I have verified the behavior both locally and on vercel.