tRPC, Nextjs GetServerSideProps and satisfies error.

Hi guys, for some reason, when i use "satisfies" operator in a Nextjs getServerSideProps, i can't use tRPC SSGHelpers or Prisma Client. I'm getting this error, "ReferenceError: createProxySSGHelpers is not defined" and crash my app. But when i remove satisfies operator, all error are gone and my app is running again. What happen here? Example: export const getServerSideProps = (async (ctx) => { const prisma = new PrismaClient(); return { props: {}, }; }) satisfies GetServerSideProps; this crashes in "ReferenceError: PrismaClient is not defined"
25 Replies
Abuzeid
Abuzeid3y ago
you have to import PrismaClient, also if you are using T3 import prisma
cje
cje3y ago
why are you creating a new prismaclient in gssp
oljimenez
oljimenezOP3y ago
was an example, i don't create Prisma Client there, but when i use the tRPC SSGHelpers give me the same error. it was imported, and what about tRPC SSGHelpers, same error when i run the app, not a type error.
Abuzeid
Abuzeid3y ago
You may have a better luck in finding the solution here https://trpc.io/docs/ssg-helpers
SSG Helpers | tRPC
createProxySSGHelpers provides you with a set of helper functions that you can use to prefetch queries on the server.
oljimenez
oljimenezOP3y ago
i already do what docs said, but the problem is with new typescript "satisfies" operator and NextJS GetServerSideProps, that give weirds interactions. Maybe is too early to use it.
Abuzeid
Abuzeid3y ago
I got you now, I had doubts that may be the problem as well, also why are you using trpc inside getServerSideProps
oljimenez
oljimenezOP3y ago
i need to prefetch some queries, just that.
esponges
esponges2y ago
Have you figured this out? @oljimenez I'm also trying to do some prefetch for an ecommerce website but I'm stuck in the ctx property that goes in the object of createProxySSGHelpers
cje
cje2y ago
just dont use satisfies imo like in this specific situation trpc and next have so much typescript generics magic going on behind the scenes
oljimenez
oljimenezOP2y ago
don't use satisfies, and do this instead. export const getServerSideProps: GetServerSideProps<ReturnPropsType> = async (ctx) => { ... };
esponges
esponges2y ago
oh, so you didn't use the ssg from the docs ?
oljimenez
oljimenezOP2y ago
yeah, i use it, look an example of const PageQuery = z.object({ room_id: z.string(), }); type ReturnProps = z.infer<typeof PageQuery>; export const getServerSideProps: GetServerSideProps<ReturnProps> = async ( ctx, ) => { const trpcServer = await trpcServerContext(ctx); const pageQuery = PageQuery.safeParse(ctx.query); if (!pageQuery.success) { return { redirect: { permanent: false, destination: "/dashboard", }, }; } await trpcServer.message.getMessagesByRoom.prefetch({ room_id: pageQuery.data.room_id, }); return { props: { trpcState: trpcServer.dehydrate(), room_id: pageQuery.data.room_id, }, }; }; this const trpcServer = await trpcServerContext(ctx); is a wrapper that i did. export const trpcServerContext = async (ctx: NextServerContext) => createProxySSGHelpers({ router: appRouter, ctx: await createContext({}), transformer: superjson, });
esponges
esponges2y ago
oh, the wrapper I'll try it with it, cuz that's where I'm stuck at 😵 thanks buddy oh but how are you passing an empty object createContext? it expects req and res sry edited
oljimenez
oljimenezOP2y ago
well, maybe is not the best way, but i did this. type NextServerContext = { req: GetServerSidePropsContext<ParsedUrlQuery, PreviewData>["req"]; res: GetServerSidePropsContext<ParsedUrlQuery, PreviewData>["res"]; }; export const trpcServerContext = async (ctx: NextServerContext) => createProxySSGHelpers({ router: appRouter, ctx: await createContext({ req: ctx.req as NextApiRequest, res: ctx.res as NextApiResponse, }), transformer: superjson, });
esponges
esponges2y ago
oh ya
oljimenez
oljimenezOP2y ago
and it works but maybe exist a better way
esponges
esponges2y ago
yes, the error is gone 😬 it ended up like that, might also make a wrapper in the future like yours
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
const ssg = createProxySSGHelpers({
router: appRouter,
ctx: await createContext({
req: ctx.req as NextApiRequest,
res: ctx.res as NextApiResponse,
}),
transformer: superjson,
});

const id = ctx.params?.productId as string;

await ssg.product.getById.prefetch({ id });

return {
props: {
dehydratedState: ssg.dehydrate(),
id,
},
};
};
export const getServerSideProps = async (ctx: GetServerSidePropsContext) => {
const ssg = createProxySSGHelpers({
router: appRouter,
ctx: await createContext({
req: ctx.req as NextApiRequest,
res: ctx.res as NextApiResponse,
}),
transformer: superjson,
});

const id = ctx.params?.productId as string;

await ssg.product.getById.prefetch({ id });

return {
props: {
dehydratedState: ssg.dehydrate(),
id,
},
};
};
thanks @oljimenez
oljimenez
oljimenezOP2y ago
yeah, the problem is that req and res from context is missing some fields . This is not secure solution i think, because typescript error is gone, but fields are still missing, also maybe you don't need them, example, req is missing body, query and env, but i don't use them in tRPC Context. I did this because i need to move fast in that moment. I will ask in the tRPC discord for a better way.
esponges
esponges2y ago
sure I understand how can we confirm that the prefetch is done serverside and not client side? If I refresh the page the isLoading prop from my query returns true and I also see the query in the network request 🤔
oljimenez
oljimenezOP2y ago
You can check if the data of the query is already there. If the data start with the value of undefined and then change, is client side, but if start with a value it means it was prefetch. Also if the query was prefetch, shouldn't show you the loading.
esponges
esponges2y ago
yeah, that's what I thought, but sadly I see the isLoading true at the beginning 😢
oljimenez
oljimenezOP2y ago
show me some code
oljimenez
oljimenezOP2y ago
change this dehydratedState: ssg.dehydrate(), for this trpcState: ssg.dehydrate()
esponges
esponges2y ago
huh, that was it! thanks again 🙏 , hopefully you get the answer from your question in the trpc grp I've already subscribed there
Want results from more Discord servers?
Add your server