blitz-js / babel-plugin-superjson-next

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

Shared SSR/SSG hooks are not transformed #121

Open alii opened 2 years ago

alii commented 2 years ago

When writing getStaticProps outside of a page file, the superjson code is never injected. For example:

// props.ts
export const getStaticProps: GetStaticProps<Props> = async ctx => {
    // ...
}

// pages/page.tsx
export {getStaticProps} from '../props';

export default function Page() {
    // ...
}
Skn0tt commented 2 years ago

Hi @alii! Would you be interested in contributing a fix for this? You should be able to add a test case for this in here: https://github.com/blitz-js/babel-plugin-superjson-next/tree/main/test

alii commented 2 years ago

Can certainly have a go, but I've never written a babel plugin before. Learning time!

Skn0tt commented 2 years ago

love it! feel free to shoot me a question if you're stuck :)

apieceofbart commented 1 year ago

For a quick hack you can just manually add superjson like so (until the PR is merged):

import { getServerSideProps } from "./getServerSideProps";

import { withSuperJSONPage as _withSuperJSONPage } from "babel-plugin-superjson-next/tools";
import { withSuperJSONProps as _withSuperJSONProps } from "babel-plugin-superjson-next/tools";

const getServerSidePropsWithSuperJSONProps =
  _withSuperJSONProps(getServerSideProps);

function Page() {
...

export { getServerSidePropsWithSuperJSONProps as getServerSideProps };

export default _withSuperJSONPage(Page);