Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
77 replies
kenny

How to SSG a page from my T3 app??

I have been following this documentation:
https://trpc.io/docs/nextjs/ssg
So I have a [chat].tsx route and I put this in the page component:
import { createServerSideHelpers } from "@trpc/react-query/server";
import {
  GetStaticPaths,
  GetStaticPropsContext,
  InferGetStaticPropsType,
} from "next";
import { appRouter } from "@/server/api/root";
import { prisma } from "@/server/db";

type ChatRouteQuery = {
  chat: string;
};

export async function getStaticProps(
  context: GetStaticPropsContext<{ chat: string }>
) {
  const helpers = createServerSideHelpers({
    router: appRouter,
    ctx,
  });
  const chatId = context.params?.chat as string;
  await helpers.chat.getOne.prefetch(chatId);
  return {
    props: {
      trpcState: helpers.dehydrate(),
      chatId,
    },
    revalidate: 1,
  };
}
export const getStaticPaths: GetStaticPaths = async () => {
  const chats = await prisma.chat.findMany({
    select: {
      id: true,
    },
  });
  return {
    paths: chats.map((chat) => ({
      params: {
        chat: chat.id,
      },
    })),
    fallback: "blocking",
  };
};

But, ctx is giving me ts errors.
No value exists in scope for the shorthand property ctx. Either declare one or provide an initializer.


And when I do ctx: {} I get this
Type '{}' is missing the following properties from type '{ session: Session | null; prisma: PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>; }': session, prismats(2739)
types.d.ts(5, 5): The expected type comes from property 'ctx' which is declared here on type 'CreateSSGHelpersOptions<CreateRouterInner<RootConfig<{ ctx: { session: Session | null; prisma: PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>; }; meta: object; errorShape: { ...; }; transformer: typeof SuperJSON; }>, { ...; }>>'


Not sure why this error is not addressed in the docs.
Reference project//github.com/trpc/examples-next-prisma-todomvc
Static Site Generation | tRPC
Was this page helpful?