blitz-js / babel-plugin-superjson-next

Automatically transform your Next.js Pages to use SuperJSON
MIT License
124 stars 15 forks source link

NextJS build fails when simple objects are returned from getStaticProps #5

Closed benjamminj closed 3 years ago

benjamminj commented 3 years ago

Hi! First off, thanks for this babel transform, I've been using it in an existing NextJS project and it is 🔥

I noticed a bug while integrating this babel transform.

After setting up the plugin in .babelrc, here's an example page /pages/no-meta.tsx:

// pages/no-meta.tsx

import { InferGetServerSidePropsType } from 'next';

export const getStaticProps = async () => {
  return {
    props: {
      rawField: 'test'
    },
  };
};

export default function NoMetaProps(
  props: InferGetServerSidePropsType<typeof getStaticProps>
) {
  return 'props.rawField is String: ' + props.rawField;
}

This page would work fine without superjson or the babel transform. But after adding the babel transform, yarn build results in the following error:

Error occurred prerendering page "/no-meta". Read more: https://err.sh/next.js/prerender-error
Error: Error serializing `.meta` returned from `getStaticProps` in "/no-meta".
Reason: `undefined` cannot be serialized as JSON. Please use `null` or omit this value all together.

Looks like the issue is coming from superjson.serialize(), which returns meta: undefined when none of the properties needs custom serialization. However, NextJS has its own validation that throws if a prop is undefined.