How to pass in data from server in Middleware mode? #14578
Unanswered
daniel-ac-martin
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello.
What is the intended way to pass data from the server into React Router when Middleware mode is enabled? My understanding is that before Middleware mode, you would put this data into the load context as it was a simple object. However with Middleware mode one would put things into the load context by using
.set()with an identifier created withcreateContext, when working across the server/app boundary these identifiers are not the same and so it does work. (It is possible to work around this issue using a string instead, but this risks clashes.)Alternatively, the documentation implies that one should instead augment the Request and deal with context only within the app:
However this approach does not work well for
.datarequests as for those routes the Request is rebuilt:react-router/packages/react-router/lib/server-runtime/single-fetch.ts
Line 138 in 0b1a878
A workaround to this would be to put the data in the headers, but that strikes me a quite hacky, and might require serialising the data only for it to be deserialised in the middleware.
For what it's worth, in my case the data is derived from authenticating the user. Perhaps my approach is wrong and I should have this authentication logic in the React Router app? (Although that seems odd as it would be exclusive to the server-side.)
I've also had a similar issue with trying to pass a nonce for a CSP, but I think this could be handled by augmenting the Request object if need be.
Thanks in advance for any help.
(Possibly related: #13331 - Perhaps the official answer is: #13331 (comment) ??)
Beta Was this translation helpful? Give feedback.
All reactions