blitz-js / legacy-framework

MIT License
3 stars 2 forks source link

Serializing dates on getServerSideProps #760

Closed dillondotzip closed 4 years ago

dillondotzip commented 4 years ago

What is the problem?

Using ssrQuery to run queries on getServerSideProps, but when passing the data back to props I get:

Error serializing `createdAt` returned from `getServerSideProps`.
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.

Steps to Reproduce

Use any page with a query:

export const getServerSideProps = async ({params, req, res}) => {

  const product = await ssrQuery(getProduct, {
    where: { id: Number(params?.productId)},
  }, {req, res})

  return {
    props: {
      product
    }
  }
}

Other

Of course I can destructure the object, but that can get tedious with a lot of keys & values.

dillondotzip commented 4 years ago

I can also do JSON.stringify() and JSON.parse(), but this makes it difficult when using typescript. Also, this seems to be apart of Next.js. According to them, they don't serialize in getServerSideProps as Date would cause hydration issues.

flybayer commented 4 years ago

@dillonraphael we have a superjson babel plugin that will enable Dates to be passed through gSSP just like you want. There's currently a bug where it's not working in blitz apps. But as soon as that's fixed, we're going to add that to Blitz.

So somewhat soon, you'll be able to do this :)