BSON serialization from getServerSideProps #13174
Unanswered
arnaudriegert
asked this question in
Help
Replies: 2 comments
-
Ok I just realized that the serializer on the API routes was just So for instance in my case this works: export const getServerSideProps = async (context) => {
return { user: JSON.parse(JSON.stringify(context.req.user)) }
} but this doesn't: export const getServerSideProps = async (context) => {
return { user: context.req.user }
} It would be nice if we could use the second syntax :) |
Beta Was this translation helpful? Give feedback.
0 replies
-
You can read more about why this is the behavior here: #12156 (comment) It's there to prevent you from introducing hydration bugs |
Beta Was this translation helpful? Give feedback.
0 replies
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.
-
Feature request
Is your feature request related to a problem? Please describe.
getServerSideProps
has been a huge help, and it would be an even bigger one if it allowed us to return objects containing BSON, for instance results from a query to a MongoDB database.Describe the solution you'd like
I would like the serializer that's called at the end of
getServerSideProps
to handle BSON just like the one called in API routes when I dores.json(...)
.Describe alternatives you've considered
Right now, in
getServerSideProps
I call an API route that does the database fetching and the serializing, so that I can insert the json from that route into the returnedprops
.But it won't work, for instance, when I want to serialize
context.req.user
which I define in my authentication middleware.The alternative would be to call manually a BSON-compatible serializer before returning the
props
. But since Next.js seems to already have on at hand for the API routes, I thought maybe we could just use the same one.Beta Was this translation helpful? Give feedback.
All reactions