t3 tRPC - CTX is undefined

Hey all. Finally checking t3 for the first time.

Question.

I was trying to add to the latest post query so that the latest post will be from the current user not all users.
 const post = await ctx.db.query.posts.findFirst({
      orderBy: (posts, { desc }) => [desc(posts.createdAt)],
      where: (posts, { eq }) => eq(posts.createdById, ctx.session!.user.id),
    });


<LatestPost> has a call to getLatest
const [latestPost] = api.post.getLatest.useSuspenseQuery();

Which is called serverside first. I am confused why, for the first serverside call, ctx in trpc is null.
To be fair this is fixed when
void api.post.getLatest.prefetch(); is called in page.tsx

But I do not understand why I have to prefetch getLatest for this to work?

Apologies if this is not the place to ask this or my question does not make sense.
Screenshot_2024-08-12_at_4.40.58_AM.png
Screenshot_2024-08-12_at_4.41.17_AM.png
Was this page helpful?