Git Product home page Git Product logo

Comments (5)

kripod avatar kripod commented on May 18, 2024 1

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)

from trpc.

kripod avatar kripod commented on May 18, 2024

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) {

from trpc.

KATT avatar KATT commented on May 18, 2024

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

from trpc.

KATT avatar KATT commented on May 18, 2024

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

from trpc.

KATT avatar KATT commented on May 18, 2024

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');

from trpc.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.