babel-plugin-superjson-next icon indicating copy to clipboard operation
babel-plugin-superjson-next copied to clipboard

Shared SSR/SSG hooks are not transformed

Open alii opened this issue 3 years ago • 4 comments

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() {
	// ...
}

alii avatar Mar 28 '22 11:03 alii

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

Skn0tt avatar Mar 30 '22 18:03 Skn0tt

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

alii avatar Apr 01 '22 10:04 alii

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

Skn0tt avatar Apr 01 '22 10:04 Skn0tt

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);

apieceofbart avatar Sep 19 '22 08:09 apieceofbart