Shared fetched data across multiple routes
Unanswered
Masai Lion posted this in #help-forum
Masai LionOP
id/[id]/page.tsx
id/[id]/(routes)/settings
id/[id]/(routes)/customization
I want to share the same data across these multiple routes:
Suppose you visited
settings
directly and then you visited customization
:*
user/id/123/settings
-> Data miss: fetches the data from the server and stores it.*
user/id/123/customization
-> Data hit: uses the previously fetched and stored data.Or, if you visit this first
user/id/123
-> Data miss: fetches and stores the data19 Replies
unstable_cache
is your answerand do not get scared with unstable in its name
its one of the most stable APIs in nextjs
@Yi Lon Ma `unstable_cache` is your answer
Masai LionOP
https://nextjs.org/docs/app/api-reference/functions/unstable_cache
Well, I want it to revalidate if they revisit the URL. I mean, next time it should automatically disappear, like session-type data.
revalidate: The number of seconds after which the cache should be revalidated. Omit or pass false to cache indefinitely or until matching revalidateTag() or revalidatePath() methods are called.
Well, I want it to revalidate if they revisit the URL. I mean, next time it should automatically disappear, like session-type data.
which i think
unstable_cache
doesn't provideyea
the react.cache also won't work cause its just for the current request
Masai LionOP
i do have an idea but idk if it will work
what abt fetching a data and storing it in a state
and passing it using context via layout across pages
well, may could work if eveything is within a client
yea that would work too but then it all would happen client side
cause you cannot access contexts serverside
@Yi Lon Ma yea that would work too but then it all would happen client side
Masai LionOP
well, only layout is in server side
but its component are all client sided
I do want to make layout client sided too but i can't access params which requires async
Masai LionOP
IT WORKS
LES GO
Masai LionOP
bot broken?