babel-plugin-superjson-next: Can't seem to get it working
I’m using superjson ^1.4.1, next 10.0.2, react 17.0.1, @babel/core ^7.12.10
I added the .babelrc file:
{
"presets": ["next/babel"],
"plugins": ["superjson-next"]
}
I reloaded my next app that I was trying to fix.
I’m using @prisma/client to query the db inside getServerSideProps where created_at/updated_at are returning Dates.
interface Task {
id string;
created_at: Date;
updated_at: Date;
...
}
export async function getServerSideProps() {
// return type of Task[] is inferred
const tasks = await prisma.user.findUnique({ where: { email }).task({ orderBy: { created_at: "desc" }, take: 10 })
return {
props: {
tasks
}
}
}
It still returned the error re: Error serializing .date
returned from getServerSideProps
unfortunately.
I commented it all out and went back to test a basic example.
export async function getServerSideProps() {
return {
props: {
date: new Date(0),
},
};
}
And it still returned
Server Error
Error: Error serializing `.date` returned from `getServerSideProps` in "/experimental/tasks".
Reason: `object` ("[object Date]") cannot be serialized as JSON. Please only return JSON serializable data types.
I did notice that when I yarn installed on another computer there was a warning re: peer dependencies, but I’m not entirely sure if it is a warning that means it will not work at all because I am using next 10 & react 17 or just a general warning.
warning " > babel-plugin-superjson-next@0.1.8" has incorrect peer dependency "next@9.X".
warning " > babel-plugin-superjson-next@0.1.8" has incorrect peer dependency "react@16.X".
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 2
- Comments: 32 (21 by maintainers)
For me its not working either. Also using prisma with next. But I dont get those errors. After adding the babel plugin, all my pages are getting ther props as
undefined
😱@allcontributors add “cyrus-za” for bug
@cyrus-za just added you manually 😅
Hey all, I have seen that somehow Next.js/blitz caches the babel config.
I have found that when testing babel stuff, you need to delete
.blitz
or.next
to see the results. Or with blitz runblitz start --no-incremental-build
https://github.com/goleary/superjson-repro/commit/d4130b78ebe1c714ee3ca1169d0598aa23d4264c works on my machine. It wasn’t a real repro, though:
package-lock.json
was missing, I had tonpm install superjson
and I needed to make this change to see something:That could have lead to some difference in our setups. Could you update the repro to be fully self-contained, so we’re on the same page?
You shouldn’t need to install this, as Next.js already comes with it. Maybe that’s a source of error?
@flybayer I only saw this now. I see @goleary already create a reproduction above. Shout if you need anything else. I’ve sine uninstalled superjson and manually converted all dates to ISO before sending props down.
repro for you here: https://github.com/goleary/superjson-repro
npx create-next-app
.getServerSideProps
toindex.ts
which spits out aDate
object.babel-plugin-superjson-next
&@babel/core
(which is not mentioned in the docs but pretty obvious that it’s needed)..babelrc
yarn dev
I still get this error:
I tested manual usage of
superjson
and it works as intended.dependencies:
env: Windows 10 & node 12.19.1