tRPC, ssr specific route
Hello. I'm migrating to tRPC and I ran into this specific issue.
In this scenario I prefetch data on server using
getServerSideProps
and then pass them to initialData
inside useQuery
. I use it for SEO purposes.
What can I do when I'm using tRPC, I'm unable to do the fetch on server using useQuery
. I know there's option ssr: true
, but I don't want to all routes to run on SSR just because of this one specific case.
Thanks.
23 Replies
I found SSG helper: https://trpc.io/docs/v10/ssg-helpers
I had to edit
CreateContextOptions
, so I was able to pass req
and res
from SSR context
Why do you pass the request and response to your
createContext
? The docs for SSG helpers does not show that it's needed. Maybe because the docs uses getStaticProps
, where request and response does not exist?It is because I pass them to the context
ctx
so I can work with them in queries and suchI do that as well, but not when/for prefetching. I'm just interested, what is your use case?
I'm off PC atm, but this is the case
it threw me an error if I didn't pass the context with req and res props
Property 'ctx' is missing in type...
or
Sorry if my question was not clear enough. I was not asking leaving out the
ctx
field when creating the helpers, but rather why you pass the request and response to the createContext
:
Why this:
ctx: await createContext({ req: context.req, res: context.res }),
and not this
ctx: await createContext(),
Or, in other words, what are you planning to do with the request and response when prefetching?
Imagine using createProxySSGHelpers
in getStaticProps
- there's no request and response there
that's why the docs for the SSG helpers also do createContext()
without passing req
and res
ohhhhh
you createContext
function's opts
argument is not optional
that's why you pass it through 😄I followed the docs where the argument is optional:
https://trpc.io/docs/v10/context#example-code
Request Context | tRPC
The createContext() function is called for each request and the result is propagated to all resolvers. You can use this to pass contextual data down to the resolvers.
Yeah, because of TS error
but what I've seen from others, they didn't have it optional tho
Yea, for Create T3 App, it is not optional. For the official docs, it is optional.
I had this:
GitHub
create-t3-app/base-context.ts at main · t3-oss/create-t3-app
Quickest way to start a new web app with full stack typesafety - create-t3-app/base-context.ts at main · t3-oss/create-t3-app
For T3, there's a helper that one can use. It already says in the comment:
But making it optional I'm running into this issue:
Not saying it should be optional, was just wondering about your use case. As you can see, in Create T3 App, there's a helper function for that. In the official tRPC docs, it is optional, because in
getStaticProps
, you don't have a request and response, so you cannot pass it to the context.In my use case I'm not using
getStaticProps
, but you made me thinking how to do it if I wanted to use itso basically like:
ctx?.res?.
How to solve the TypeScript here?
of descructing possible undefined type
const { req, res } = opts || {};
Does this make sense?
return await createContextInner(!req || !res ? undefined : { req, res });
I just looked at an official tRPC example, where the opts are also mandatory:
https://github.com/trpc/examples-next-prisma-starter/blob/next/src/server/context.ts
GitHub
examples-next-prisma-starter/context.ts at next · trpc/examples-nex...
mirror of https://github.com/trpc/trpc/tree/main/examples/next-prisma-starter - examples-next-prisma-starter/context.ts at next · trpc/examples-next-prisma-starter
My opts were always optional, as I do authentication on a per-request basis instead of globally for all requests, so I'm not the right person to answer your question.
There is another official example that is dedicated to the SSG helpers where
opts
is optional though. I think it really depends on how you use the request in your queries, are they really always necessary?
https://github.com/trpc/examples-next-prisma-todomvc/blob/next/src/server/context.tsGitHub
examples-next-prisma-todomvc/context.ts at next · trpc/examples-nex...
mirror of https://github.com/trpc/trpc/tree/main/examples/next-prisma-todomvc - examples-next-prisma-todomvc/context.ts at next · trpc/examples-next-prisma-todomvc
Mmm, as I said I'm not planning to use SSG, so in my particular case, I don't need them optional, but it's good thing to keep eyes on future projects