Share state between server route and SSR context
I have a server route in a nuxt 3 application that has SSR enabled.
When someone posts to that route i want to set a flag to true.
Then when another requests comes in to render a page i want to read that flag and if it is true i want to perform an action and then reset the flag.
From my exploration of nuxt, nitro and h3 this does not seem to be possible.
Is it possible to share state like this, or should i try to find another solution to my problem?
5 Replies
maybe explain your use case then we can think of solutions. I would go for a cloud storage provider and save it there. Then retrieve your flag with the second call from storage
Where is it you want to perfrom "an action"?
In this case a 3pp has decided to use an in-memory cache that we have to purge using a webhook.
Our plan was to use a server route "/api/purge" to run the 3pp's built in purge method. But it turned out that there was no support for running that 3pp hook in a server route.
So our next idea was to set some kind of state from the server route that could be read by our SSR routing middleware and call for the purging of the cache from there, then reset the state flag.
The sharing of state is what we cannot figure out.
So right now the "where" is a nuxt route middleware.
What we managed to implement was a view route using a page that is never going to be accessed by a user
pages/purge.ts
. In the routing middleware we check if we're on the server during an SSR request. Then we use the private runtime config to validate the request signature and purge the servers 3pp's cache.
However this isn't very clean and we'd like to separate our service integration flow from our users if possible.But it turned out that there was no support for running that 3pp hook in a server route.why not?
What package this is and why the maintainer has chosen not to support using it in server routes is really an implementation detail that isn't relevant to my issue at hand, other than the fact that it isn't supported.
There's a clear question stated about sharing state between a server route and a request to SSR a page.
Lets keep it to that.
Unfortunately this would mean that we'd have to perform this check pretty often (for each non-edge-cached SSR result), and since it would be render-blocking i would like to avoid it. We're running this application at scale with a lot of users so each request counts.