App router: Having trouble with sharing state between server-side layout, pages and actions
Unanswered
Grand Griffon Vendéen posted this in #help-forum
Grand Griffon VendéenOP
I'm stuck on this for 3 days now, and at least in my old stack (NodeJS), this was trivial...
I have some state that is unique for a user session, it is partially in the cookies, partially in the path params.
Before every route handler, I want to "do a bunch of stuff. And then put a resulting object in a shared cache that the route handler will use.
In NodeJS, I use middleware for that.
My understanding is that NextJS middleware is the wrong way to do it, but using react-cache and the layout is the right approach
So I used https://github.com/manvalls/server-only-context which wraps react-cache.
And my layout has:
where Nile Server populates the react cache.
This works great for the pages, I get do
But, I also use NextJS actions. And it looks like this does not run before the actions.
So if I try to get the state from the cache in the action, it is empty.
Any suggestions how to extend this lovely pattern to NextJS actions?
I have some state that is unique for a user session, it is partially in the cookies, partially in the path params.
Before every route handler, I want to "do a bunch of stuff. And then put a resulting object in a shared cache that the route handler will use.
In NodeJS, I use middleware for that.
My understanding is that NextJS middleware is the wrong way to do it, but using react-cache and the layout is the right approach
So I used https://github.com/manvalls/server-only-context which wraps react-cache.
And my layout has:
<NileServer tenantId={tenantId} userId={userId} userToken={token}>
{children}
</NileServer>where Nile Server populates the react cache.
This works great for the pages, I get do
getNile() and get the right object from react cache. But, I also use NextJS actions. And it looks like this does not run before the actions.
So if I try to get the state from the cache in the action, it is empty.
Any suggestions how to extend this lovely pattern to NextJS actions?