Skip to content

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

Closed
kripod opened this issue May 16, 2021 · 5 comments
Closed

Loosen trpcNext.CreateNextContextOptions type for better SSR support #366

kripod opened this issue May 16, 2021 · 5 comments

Comments

@kripod
Copy link
Contributor

kripod commented May 16, 2021

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;`
    };
@kripod
Copy link
Contributor Author

kripod commented May 16, 2021

With this change, the parameter given to createContext should always be required:

- export async function createContext(opts?: trpcNext.CreateNextContextOptions) {
+ export async function createContext(opts: trpcNext.CreateNextContextOptions) {

@KATT
Copy link
Member

KATT commented May 16, 2021

You know, you can simply do ssr: true in the top level HOC to enable ssr, you don't need to do any manual ssr stuff unless you specifically want ssg

See https://trpc.io/docs/ssr

On my phone, will answer properly later

@KATT
Copy link
Member

KATT commented May 16, 2021

The next-hello-world example has ssr enabled if you want a reference

@kripod
Copy link
Contributor Author

kripod commented 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)

@KATT
Copy link
Member

KATT commented 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

export const createContext = async (
opts?: trpcNext.CreateNextContextOptions,
) => {
return {
req: opts?.req,
prisma,
task: prisma.task,
};
};
export type Context = trpc.inferAsyncReturnType<typeof createContext>;

usage

const ssg = createSSGHelpers({
router: appRouter,
transformer,
ctx: await createContext(),
});
await ssg.fetchQuery('todos.all');

@KATT KATT closed this as completed May 16, 2021
@trpc trpc locked and limited conversation to collaborators May 16, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

2 participants