Next.js Discord

Discord Forum

Sharing User Data Between Server Components of Different Pages in Next.js 14

Unanswered
Abu Zain posted this in #help-forum
Open in Discord
I am using Next.js 14 with the app router. In my main layout file, I am fetching user data using a server component and then sharing that data with a client component. I have a context provider that passes this data to the context, allowing me to easily access the user data across all client components on different pages.

However, I am facing an issue: while I can easily access the user data from the context in client components on all pages, I need to access this data in a few server components on other pages as well. How can I achieve this? Should I use cookies on the server side for this, or is there a better approach?

32 Replies

Madeiran sardinella
Why you don't just fetch the user data in the server components you need it?
then I have to fetch that data many times and I have to fetch it on each page. Which is not performance efficient
Put the user information in a cookie and access it in the server component
Server components can reading incoming cookies.
The implication of fetching the data not being performant is still relevant though memorizing does not stop the cost associated with fetching data every time to go away. Unless I’m missing something?
@English Lop If you need to use the same data (e.g. current user) in multiple components in a tree, you do not have to fetch data globally, nor forward props between components. Instead, you can use fetch or React cache in the component that needs the data without worrying about the performance implications of making multiple requests for the same data. This is possible because fetch requests are automatically memoized.
Please note that user data is needed on all pages. If I use the cache, it will not help me because it caching the requests on the same render, but I need this data on all pages, which means different renders. One thing to keep in mind is that I am using server actions for data fetching, not fetching data directly in the components.
@Jboncz @English Lop
Well you shouldn’t be using server actions for fetching. And also you need to store the data in a cookie
If you want to prevent constant fetches.
There’s no magical way to store the user context across renders without some intermediary. You either fetch it store it on the client side. You can even encode the cookie so the data is able to be validated in your server component if you want
There isn’t one… there are only certain ways to persist data when hosting in a serverless environment. Cookies, local storage, or db. Db you have to fetch from, local storage isn’t available to server components.
Or url params but that would be silly to use
@riský back me up or correct me?
@Jboncz There isn’t one… there are only certain ways to persist data when hosting in a serverless environment. Cookies, local storage, or db. Db you have to fetch from, local storage isn’t available to server components.
Yes, you are right; using cookies is a way to go, but I am also curious about the performance because my project is quite complex and large, so performance is crucial for me.
But… none of that changes the underlying technology, there isn’t another option, there are other options on a non-server-less environment but that’s not relevant.
@Jboncz <@657067112434499595> back me up or correct me?
can you summarize as my brain isnt braining rn :(
What’s the best way to persist user information across renders without fetching from the db to get the info. In server components.
yeah i would say cookies, or if userid and unique identifier already could use unstable_cache (and if not serverless, just good old object cache)
Yeah if it wasn’t serverless of course store it in memory. I don’t ever recommend unstable cache for anything prod, but it’s an option.
Cookie is the safest approach.
My cookies are tied directly to the user session in the db, so when I do need to validate on the backend I can validate against the session and bobs your uncle.
my question is what is so important to store on server side, but shouldnt be saved in database
Nah he just doesn’t want to fetch from the db every time. lol
Idk. Just can’t give a better answer than he’s gotten 🤷 I needed to make sure I wasn’t lying to him.
Here is a summary of my problem.
I have a frontend in Next.js and a backend in NestJS. I don't have access to the database in Next.js due to some safety concerns (we want to keep the backend and frontend separate). When the website loads, I get data by calling the API endpoint of the NestJS backend using fetch server actions. Then, I set that user data in the context as default data so that all components can use it, which is working fine.

However, on some pages, I need to fetch data based on the user data, such as landlord properties. I can't access that user data directly there, so I end up having to refetch it. I want to avoid this refetching. @Jboncz @riský
🍪
@Jboncz 🍪
haha 😅 Got it
I’m just being straight with you, unless there is some magical technology I don’t know about your kinda out of options.
But anyways I’ll let you seek out other answers if you want let me know if you figure something out!