should I use a trpc request when using getStaticPaths()?

Should I do a trpc request instead of getStaticProps or is this template fine? If the template is fine, how do I get types through my props?
40 Replies
julius
julius3y ago
ssg helpers are good for this usecase https://trpc.io/docs/v10/ssg-helpers
Johnson
JohnsonOP3y ago
I took a look at those before and was hoping I could not use them cause they seem really complicated lol but, thx! I'll take a look again
julius
julius3y ago
GitHub
trpc/[filter].tsx at 58bca24812ce6b3be1cc02a56128e09724c45687 · trp...
🧙‍♀️ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. - trpc/[filter].tsx at 58bca24812ce6b3be1cc02a56128e09724c45687 · trpc/trpc
Johnson
JohnsonOP3y ago
ty so much any idea why I cant import the ssgHelper? I only get: Cannot find module '@trpc/react-query/ssg' or its corresponding type declarations.ts(2307) thats the import: import { createProxySSGHelpers } from '@trpc/react-query/ssg';
julius
julius3y ago
what version?
Johnson
JohnsonOP3y ago
of trpc?
julius
julius3y ago
@trpc/react was renamed to @trpc/react-query on beta.21 so if you're lower than that
Johnson
JohnsonOP3y ago
julius
julius3y ago
do import { createProxySSGHelpers } from '@trpc/react/ssg';
Johnson
JohnsonOP3y ago
seems like I am on beta 17 works god how do you know all this xD its impressive
julius
julius3y ago
im on the trpc core team too so i'm up to date on the releases there
Johnson
JohnsonOP3y ago
ahhh nice
Johnson
JohnsonOP3y ago
yeah I was about to ask if it makes sense to migrate
Johnson
JohnsonOP3y ago
Johnson
JohnsonOP3y ago
Johnson
JohnsonOP3y ago
I seem to have issues with having to pass an argument to the context to me it seems like that was optional in the old version, idk what went wrong here
julius
julius3y ago
https://beta.create.t3.gg/en/usage/trpc#-servertrpccontextts for the ssg helpers, you'd use the createContextInner function
ctx: await createContextInner({ session: null }); // <-- pass session too
ctx: await createContextInner({ session: null }); // <-- pass session too
Create T3 App
tRPC 🚀 Create T3 App
The best way to setup an opinionated, full-stack, typesafe Next.js project
Johnson
JohnsonOP3y ago
ahh so I was reading from a v10 document template probably would you say it makes sense to upgrade now? because I feel like I am writing dead code rn or rather code that has to be replaced
julius
julius3y ago
the upgrades are really small, generally only 1 PR, some days there are multiple
Johnson
JohnsonOP3y ago
hmmm okay
nexxel
nexxel3y ago
julius is a god
Johnson
JohnsonOP3y ago
so the idea here is that I use the trpc queries I made (which access the database), to then populate all the generated sites with the fetched data, right?
Johnson
JohnsonOP3y ago
but I don't entirely get the difference between fetch and prefetch, am I not going to fetch anyway when the sites are actually generated?
julius
julius3y ago
yep, you'd do a fetch in the getStaticProps and then a useQuery on the component that would fetch the data on server and put it in the queryCache for the client to grab you want fetch in order for the site to be static, prefetch is when you dont need the data right away
Johnson
JohnsonOP3y ago
ah and because I would usually fetch something like a users posts, I want the data => I want to fetch and not prefetch or not even the users posts could just fetch all posts
julius
julius3y ago
yea tbh i dont really know the difference, just that fetch returns the value and prefetch doesn't, so if you want to render a site statically you need to fetch
Johnson
JohnsonOP3y ago
ke
julius
julius3y ago
prefetch would require more javascript so that the value is put into the cache etc yaddiayadda xD for example, our ssg test here https://github.com/trpc/trpc/tree/next/examples/.test/ssg runs playwright with javascript disabled if we would change the fetch to prefetch then the data wouldn't be there since javascript is disabled
Johnson
JohnsonOP3y ago
kinda interesting not gonna lie can I ask you how you would approach a certain problem? Because I want to know if the approach I am thinking of actually works
julius
julius3y ago
ask away
Johnson
JohnsonOP3y ago
nice so basically: users make posts, posts can be searched for, posts are generated via ssg and: users can edit their own posts via an edit button now I am thinking of how to verify when a user is actually on the generated site of their own post I have 2 approaches in mind: 1: when the post is created, put the userId as 'dead weight', as additional data on the generated site, so then I can check the current session.id against that everytime someone connects to the post 2: everytime someone connects to the post, check usernames and make usernames unique but the more I think about it, approach 2 is kinda like 1 except for usernames I might be confused here lol actually I think 1. is the way to go now that I typed it out xD
julius
julius3y ago
i am not sure i get what you're asking 🤔
Johnson
JohnsonOP3y ago
ye I might not have explained that very well
julius
julius3y ago
you mean like verifying that the user wrote that post?
Johnson
JohnsonOP3y ago
well I need to render the edit button on the page if the user who actually made that post re-visits it so yeah
julius
julius3y ago
i'd do something like this
const PostPage = () => {
const { data: post } = trpc.post.byId.useQuery({ id: "some-id" });
const { data: session } = trpc.auth.session.useQuery(); // or useSession whichever you use
const canEdit = session?.user && session.user.id === post?.id

return (
...
{canEdit && <EditButton />}
);
};
const PostPage = () => {
const { data: post } = trpc.post.byId.useQuery({ id: "some-id" });
const { data: session } = trpc.auth.session.useQuery(); // or useSession whichever you use
const canEdit = session?.user && session.user.id === post?.id

return (
...
{canEdit && <EditButton />}
);
};
Johnson
JohnsonOP3y ago
yeah and 'some-id' would be passed down as props from getStaticProps I assume (during the ssg I guess)
julius
julius3y ago
the same id of the post you're fetching
Johnson
JohnsonOP3y ago
yeah I think I get it now thats smort so every time someone connects, based on the link that was generated (postID) I make a request for the post (as data I can work with) and then I can access post.userID ty so much for the help again
Want results from more Discord servers?
Add your server