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,
},
};
};
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?
8 Replies
Shoodey
ShoodeyOP•2y ago
kind bump on this
cje
cje•2y ago
SSG Helpers | tRPC
createProxySSGHelpers provides you with a set of helper functions that you can use to prefetch queries on the server.
rykuno
rykuno•2y ago
What I found way easier than SSG Helpers is just restructuring your TRPC endpoint to be a proxy in a sense. All my logic inside my trpc handlers are abstracted to a different file so I can simply call that function server side from nextjs
Shoodey
ShoodeyOP•2y ago
do you have a public repo or examples please?
cje
cje•2y ago
you literally just write a function and call it trpc and gssp both run on the server so they can call the same functions
Shoodey
ShoodeyOP•2y ago
aight will try that for now - ssg didnt work for me as my content behind dynamic route is very specific to users and doesnt make sense ssg it? at least it is painfully slow and cant be built/cached or smth? thanks both it just looks cool when u trpc all the way 😄
cje
cje•2y ago
if its very user specific you cant ssg it or you can ssg the static bits and then hydrate a query or something like that btw ssg helpers work in ssr also
Shoodey
ShoodeyOP•2y ago
im at my depth, this is a very tricky use case i guess? would u be intersted in hearing details maybe u could help me figure out the missing piece
Want results from more Discord servers?
Add your server