Loosen `trpcNext.CreateNextContextOptions` type for better SSR support
See original GitHub issueI just started using trpcNext.CreateNextContextOptions
and noticed that it’s typed as follows:
type CreateNextContextOptions = {
req: NextApiRequest,
res: NextApiResponse,
};
While it works great for API requests, the context
that gets passed to getServerSideProps
only contains a portion of this information – the basis of NextApiRequest
and NextApiResponse
objects:
import type { IncomingMessage, ServerResponse } from "http";
type GetServerSidePropsContextOptions = {
req: IncomingMessage & { cookies: NextApiRequestCookies },
res: ServerResponse,
};
In order to support SSR better and keep the API context versatile, I propose a context in which only the req
object is mandatory during SSR, as responses shouldn’t be altered in that case. API functionality is kept intact:
type CreateNextContextOptions =
| {
req: NextApiRequest;
res: NextApiResponse;
}
| {
req: IncomingMessage & { cookies: NextApiRequestCookies };
// Altering responses during SSR isn’t a good idea:
res?: never; // Previous thought: `res?: ServerResponse;`
};
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
Don't get cookies in trpc context - Stack Overflow
Let me copy over the answer from github for progeny. It's an SSR thing. Headers aren't copied over by default so you have...
Read more >The trpc from trpc - Giter VIP
Loosen `trpcNext.CreateNextContextOptions` type for better SSR support. I just started using trpcNext.CreateNextContextOptions and noticed that it's typed ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I know about the
{ ssr: true }
option, but that relies upongetInitialProps
, which breaks SSG for static pages. I wouldn’t like to use that option due to this.I think the default context parameter should be adjusted regardless of that, even when
{ ssr: true }
is specified. I proposed not to forwardres
during SSR, but always forwardreq
so that cookies can be read (e.g. for auth)I think you have a good point but we’d need a third constant to make it easy to differentiate between them:
The way I’ve solved this myself when I wanted similar stuff in my
getStaticProps
-fns is to make opts optional so I can simply call mycreateContext
from the function.Example
context fn
https://github.com/trpc/trpc/blob/b0ad15ef95b8d7d0b7ac8747df54d5aa08154550/examples/next-prisma-todomvc/pages/api/trpc/[trpc].ts#L10-L19
usage
https://github.com/trpc/trpc/blob/b0ad15ef95b8d7d0b7ac8747df54d5aa08154550/examples/next-prisma-todomvc/pages/[filter].tsx#L340-L346