isSerializable does not include Date type #11498
-
Is it possible that Date as type is missing from this method? I am getting an error when trying to export my site as a static one and when the object contains date values. Temporary workaround is to cast the date type as a string |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 17 replies
-
|
Beta Was this translation helpful? Give feedback.
-
See #13696 for a proposed solution to this issue |
Beta Was this translation helpful? Give feedback.
-
Hi @Timer If I have read it correctly, NextJs is always using And The purpose of this existing Date validation seems suppose to act as a server-client API typing glue. But the current way is more like an unnecessary API restriction. And we have date fields on every record, this is troublesome to manage and I believe it's bad for performance. As we are using TypeScript, we'd prefer to leave the typing concerns to it if possible. Such as adding an enhanced version of Do you see any problem with this approach? I understand that not everyone is using TypeScript with NextJs. Can you allow users to opt-out the development-time Date restriction, so that we can implement the mapped type ourselves? |
Beta Was this translation helpful? Give feedback.
-
Hi @Timer, @timneutkens, Although the question is marked as answered, for me it is not clear yet what would be the best pattern to use. What I'm thinking of now is to create DTO objects that mimic the database models but where the date fields are strings. Then write some mappers to transform the database models to the "JSON" DTO's and pass these from Is that a pattern others are using as well? Just JSON stringifying won't help as I will lose type safety in the React frontend components. It is a pity that all online examples, blogs don't use dates - on purpose? - so the examples look really simple and don't give any hints what would be a pattern to solve this. |
Beta Was this translation helpful? Give feedback.
-
It is almost 2022 and I have to workaround serializing a date. Pls! |
Beta Was this translation helpful? Give feedback.
-
Since Babel is replaced by SWC in NextJS 12, the |
Beta Was this translation helpful? Give feedback.
-
since no one mentioned it and i've seen people saying that I am using prisma so my function looks like this export async function getServerSideProps() {
const user: User[] = await prisma.user.findMany()
return {
props: {
initial: JSON.parse(JSON.stringify(user)),
},
}
} and this is DATETIME type in my schema is what was causing the problem in my case. model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
email String @unique
password String
role Role @default(SIMPLE)
createdAt DateTime @default(now()) @map(name: "_createdAt")
updateAt DateTime @updatedAt @map(name: "_updatedAt")
info UserInfo?
} |
Beta Was this translation helpful? Give feedback.
-
For others who have come here recently, the |
Beta Was this translation helpful? Give feedback.
-
This is the solution: https://github.com/blitz-js/next-superjson-plugin |
Beta Was this translation helpful? Give feedback.
Date
is serialized to a string and then on the client-side would cause a hydration error asDate
is expected to be a date object in most cases.