Is request memoization disabled in Next Dev ?
Unanswered
Eulophid wasp posted this in #help-forum
Eulophid waspOP
It seems to fire the request twice as the logs in my backend. What causes this ?
36 Replies
@Eulophid wasp It seems to fire the request twice as the logs in my backend. What causes this ?
Sun bear
Hi @Eulophid wasp
@Sun bear Hi <@695333753005801541>
Sun bear
I am confused your issue, please can you tell me in detail?
@Sun bear I am confused your issue, please can you tell me in detail?
Eulophid waspOP
Request memoization is supposed to cache my request in the same render pass so it doesn’t go to the data cache or data source. but in my case it is not doing that
Sun bear
it seemed to work in next 14 at least based on the fetch logs output
@Sun bear it seemed to work in next 14 at least based on the fetch logs output
Eulophid waspOP
In my case it is not working even in next 14. I read on some github open issues that it works in production mode but not in dev mode
@Sun bear have the same issue
https://discord.com/channels/752553802359505017/1307081409697026179/1307081409697026179
Eulophid waspOP
just took a look at your case, in my case the fetch logs show one GET request while my backend shows more than one
Sun bear
yeah I think that may be happening for me as well, I just never looked at the server logs on 14.
so I guess at least they fixed the logs on 15 🤷♂️
Eulophid waspOP
the caching behaviour just gets weirder the more you dive into it hahaha
Sun bear
yeah, this one is really annoying because the request memoization is sort of a fundamental part of the programming model to me
if it doesnt work then the whole thing doesnt work
Eulophid waspOP
did you give it a shot in prod mode ?
Sun bear
havent had a chance to really try it
Eulophid waspOP
try building it and starting that, my hopes are that it will work
Sun bear
i at least see that there's only one request in the "request metrics" section of the Vercel logs
but considering the local logging was apparently broken who knows if that can be trusted
Eulophid waspOP
requests that use tokens and cookies can not be cached right ?
Sun bear
Im not 100% sure I think you can force it with force-cache
or barring that you can also wrap it in
unstable_cacheEulophid waspOP
the thing is i am not sure how to try out things if some thing dont work in dev mode but might work in prod mode
Sun bear
docs on it are unclear enough that ive found it helpful to just dig into the actual source of the patched fetch
https://github.com/vercel/next.js/blob/1bf026610381b0bf4d5de2886f8fab9e5d50ad4f/packages/next/src/server/lib/patch-fetch.ts#L379
https://github.com/vercel/next.js/blob/1bf026610381b0bf4d5de2886f8fab9e5d50ad4f/packages/next/src/server/lib/patch-fetch.ts#L379
Eulophid waspOP
oh no that would be a nightmare
Sun bear
i think that the cache option being force-cache overrides the auto caching behaviour that would have happened on 14 if there were cookies or authorization headers present
but also auto caching by default was removedi n 15
@Eulophid wasp oh no that would be a nightmare
Sun bear
welcome to next 🙃
@Sun bear i think that the cache option being force-cache overrides the auto caching behaviour that would have happened on 14 if there were cookies or authorization headers present
Eulophid waspOP
one one side of the documentation it says
then on another one it says this
Whether the data is cached or uncached, the requests are always memoized to avoid making duplicate requests for the same data during a React render pass.then on another one it says this
If you are using fetch, requests can be memoized by adding cache: 'force-cache'. This means you can safely call the same URL with the same options, and only one request will be made.does that sound contradicting to you ?
these are both on the next 15 docs, so up to date
Sun bear
yep sounds contradictory
@Sun bear i think that the cache option being force-cache overrides the auto caching behaviour that would have happened on 14 if there were cookies or authorization headers present
Eulophid waspOP
when i reload the page, if the fetch is without cookies in headers. The cache is always HIT if i set the cache to force cache. While if i do the same with a fetch with cookies and headers, It is always a miss
also more here :
so i do not understand how is that related to memoization
options.cache : Configure how the request should interact with Next.js Data Cache.so i do not understand how is that related to memoization
i guess the only way it to experiment and try all combinations until something works out
i might try spinning up a quick next js build on the side with a similar fetch and run in prod to see if memoization works. I will keep you updated
Sun bear
yeah Im basically seeing the memoization not working in general, though I think in my case that's also being influenced by the fact that I'm passing an "options" object which is a different object for each call
if we assume that memoization (if it exists) works the same as the react "cache" function, then it wouldn't memoize if any options object is passed unless that object is referentially equal to the other calls