Should a <ReturnType typeof fn> type declaration of a function used in getServerSideProps cause the function to be bundled client-side? #14324
-
Hi friends, See here: https://next-code-elimination.now.sh/s/nP8kScwwq I'm unsure if this is a bug or expected behaviour because I'm not familiar with the intricacies of bundling types + next's client side code elimination. The reason this came up is because I'm using Prisma in Defining Prisma code: https://next-code-elimination.now.sh/s/3byxqXdyx import { PrismaClient, PromiseReturnType } from "@prisma/client"
import { GetServerSideProps } from "next"
import React from "react"
const prisma = new PrismaClient()
async function getOrg(slug: string | string[] | undefined) {
if (!slug || (slug && typeof slug !== "string")) {
return null
}
return await prisma.organisation.findOne({
where: {
slug,
},
})
}
type Organisation = PromiseReturnType<typeof getOrg>
type PageProps = {
organisation: Organisation
}
export default function Page(props: PageProps) {
return <div>{props.organisation.name}</div>
}
export const getServerSideProps: GetServerSideProps<PageProps> = async ({
query: { slug },
}) => {
const organisation = await getOrg(slug)
return {
props: {
organisation,
},
}
} If it's not a bug, is there a better way of handling this? Thanks 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hi @leerob, sorry to include you out the blue, but I noticed you're doing the prisma and next workshop for Prisma day. Any ideas on the above? Not being able to use |
Beta Was this translation helpful? Give feedback.
-
You can check if it's tree shaken here: https://next-code-elimination.now.sh/, looks like your code is not tree shaken currently because it imports a type from the prisma module 🤔 However looking at the code you probably want to use https://nextjs.org/docs/basic-features/data-fetching#typescript-use-getserversideprops import { PrismaClient } from "@prisma/client"
import { GetServerSideProps, InferGetServerSidePropsType } from "next"
import React from "react"
const prisma = new PrismaClient()
async function getOrg(slug: string | string[] | undefined) {
if (!slug || (slug && typeof slug !== "string")) {
return null
}
return await prisma.organisation.findOne({
where: {
slug,
},
})
}
export default function Page(props: InferGetServerSidePropsType<typeof getServerSideProps>) {
return <div>{props.organisation.name}</div>
}
export const getServerSideProps: GetServerSideProps<PageProps> = async ({
query: { slug },
}) => {
const organisation = await getOrg(slug)
return {
props: {
organisation,
},
}
} |
Beta Was this translation helpful? Give feedback.
-
Duplicate of #12756, but I've opened an issue with OPs reproduction! |
Beta Was this translation helpful? Give feedback.
You can check if it's tree shaken here: https://next-code-elimination.now.sh/, looks like your code is not tree shaken currently because it imports a type from the prisma module 🤔
However looking at the code you probably want to use https://nextjs.org/docs/basic-features/data-fetching#typescript-use-getserversideprops
InferGetServerSidePropsType
which is nicer anyway as it automatically infers the types from the prisma fetching you're doing