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: