-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loosen trpcNext.CreateNextContextOptions
type for better SSR support
#366
Comments
With this change, the parameter given to - export async function createContext(opts?: trpcNext.CreateNextContextOptions) {
+ export async function createContext(opts: trpcNext.CreateNextContextOptions) { |
You know, you can simply do On my phone, will answer properly later |
The next-hello-world example has ssr enabled if you want a reference |
I know about the I think the default context parameter should be adjusted regardless of that, even when |
I think you have a good point but we'd need a third constant to make it easy to differentiate between them: type CreateNextContextOptions =
| {
type: 'api',
req: NextApiRequest;
res: NextApiResponse;
}
| {
type: 'ssr',
req: IncomingMessage & { cookies: NextApiRequestCookies };
// Altering responses during SSR isn’t a good idea:
res?: never; // Previous thought: `res?: ServerResponse;`
}; The way I've solved this myself when I wanted similar stuff in my Examplecontext fntrpc/examples/next-prisma-todomvc/pages/api/trpc/[trpc].ts Lines 10 to 19 in b0ad15e
usagetrpc/examples/next-prisma-todomvc/pages/[filter].tsx Lines 340 to 346 in b0ad15e
|
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
I just started using
trpcNext.CreateNextContextOptions
and noticed that it’s typed as follows:While it works great for API requests, the
context
that gets passed togetServerSideProps
only contains a portion of this information – the basis ofNextApiRequest
andNextApiResponse
objects: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:The text was updated successfully, but these errors were encountered: