Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
14 replies
Shoodey

trpc call inside getServerSideProps?

(create-t3-turbo) I have a dynamic route for users' stuff, stuff/xyz they can they go to stuff/xyz/doStuff or stuff/xyz/doOtherStuff

I need to validate xyz that it exists in my DB - then redirect to 404 if fails, so far ive done it with prisma client directly (inspired from zapdos kinda)

export const getServerSideProps: GetServerSideProps = async ({ params }) => {
  if (!params || !params.xyz || typeof params.xyz !== "string") {
    return {
      notFound: true,
    };
  }

  const stuff = await prisma?.stuff.findFirst({
    where: {
      xyz: params.xyz,
    },
  });

  if (!stuff) {
    return {
      notFound: true,
    };
  }

  return {
    props: {
      stuff: transformer.serialize(stuff).json,
    },
  };
};


which works fine tbh - but i dont like how i need to import { Stuff } from ".prisma/client"; for my prop type and how im not using trpc for this (tbh) - is there a way to use trpc here or simply a better way?

does it make sense to just trpc in client and redirect on useEffect + useRouter?
Was this page helpful?