Next.js Discord

Discord Forum

Request-specific Key/Value Cache

Answered
Broad-snouted Caiman posted this in #help-forum
Open in Discord
Broad-snouted CaimanOP
Is there a way to cache specific values as key/value pairs for the life of a request? Similar to cache for functions, but key-driven. I'd like to set some values in a parent Layout (on the server) and then access those values in a deeply nested child component (on the server) without having to prop drill. Traditional context won't suffice here.
Answered by B33fb0n3
Try to avoid that as much as possible but if you still can't figure a way out to solve it without prop drilling & server context, I prefer using this server context: https://www.npmjs.com/package/server-only-context
View full answer

11 Replies

Answer
@Broad-snouted Caiman solved?
Broad-snouted CaimanOP
Ah, sort of. Sorry, away for work.
I’m curious why this pattern should be avoided. Do we make the React lifecycle less efficient or something?
@Broad-snouted Caiman I’m curious why this pattern should be avoided. Do we make the React lifecycle less efficient or something?
Nah, I also don’t know why, but in my mind it’s bad to do it like that and normally there are always ways to do it without (and still no prop drilling). But yea.. I also got a case once, where I needed it and I used it. It worked fine and I was happy 🙂
Broad-snouted CaimanOP
The server context package is alright, though I consider it unfavorable to have to set the context on both the page and layout.
Do you know what happened to the native server context API?
Yea... whatever you do with it: be careful
@Broad-snouted Caiman Do you know what happened to the native server context API?
I havent used the native server context api in the past and don't know what happend with it
@Broad-snouted Caiman solved?
If that's not the solution, feel free to mark a different message as solution