question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loosen `trpcNext.CreateNextContextOptions` type for better SSR support

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kripodcommented, May 16, 2021

I know about the { ssr: true } option, but that relies upon getInitialProps, 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 forward res during SSR, but always forward req so that cookies can be read (e.g. for auth)

0reactions
KATTcommented, May 16, 2021

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 getStaticProps-fns is to make opts optional so I can simply call my createContext 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found