Next.js Discord

Discord Forum

Why isn't Request Memoization working as I'd expect?

Unanswered
IPJT posted this in #help-forum
Open in Discord
Hi all,
I'm trying to wrap my head around caching in Next.js. I've decided to try and properly give it a shot and understand the fundamentals. I've started with Request Memoization and Data Cache. While trying to understand this I've tried to "see it myself" by logging some statements etc. However, it's not behaving as I'd expect. I'd appreciate your help understanding why. Thank you in advance!

So my setup is:
- NextJs-version: v14.2.15
- React-version: v18.2.0

- Three layers of nested components - Root Layout, Nested Layout, Nested Page - all are server components and async
- All of these components call the exact same Route Handler (/api/test) using the fetch method (GET)

If I've understood Request Memoization correctly the Route handler should only be invoked once as the request should be memoized after the first invocation. However, according to the logs (see the yellow warning logs in the attached image), it is invoced twice. Additionally I think the reason why it's not invoked a third time is not due to the request being memoized but due the it hitting the Data Cache. Any takes on this?

Additionally, I've appreciate any advice in terms of how to "visualize/track" outgoing requests from the Nextjs server environment. I've set logging: {fetches: {fullUrl: true, }} in next.config.mjs but as seen in the attached screenshot "GET http://localhost:3000/api/test" was only logged once while i know it was invoced twice.. In the browser it's super easy to see when we're crossing "network boundaries" by inspecting the network tab. I'd like something similar for my nextJs server running locally.

Thanks a lot! All help is highly appreciated

0 Replies