VilianV
tRPC3y ago
23 replies
Vilian

nextjs app router `fetchRequestHandler`'s createContext doesn't run.

Hey there,

I have the following code:
// /src/app/api/trpc/[trpc]/route.ts
import { fetchRequestHandler } from '@trpc/server/adapters/fetch';
import { appRouter } from '~/trpc/root';
import { ENV } from '~/env';
import { createTRPCContext } from '~/trpc/trpc';

const handler = (req: Request) =>
  fetchRequestHandler({
    req,
    router: appRouter,
    endpoint: '/api/trpc',
    onError: ({ path, error }) => {
      if (ENV.NODE_ENV !== 'development') return;
      console.error(
        `❌ tRPC failed on ${path ?? '<no-path>'}: ${error.message} `
      );
    },
    createContext: createTRPCContext,
  });

export { handler as GET, handler as POST };
Was this page helpful?