PinkiePie
PinkiePieโ€ข2mo ago

trpc doesn't work after migrating to next 15

When i upgraded to Next 15 (pages router), all trpc routes fails with status 500 on production. The result message is {"error":{"json":{"message":"req.socket.once is not a function","code":-32603,"data":{"code":"INTERNAL_SERVER_ERROR","httpStatus":500}}}}. I don't really understand if it's TRPC issue, Next issue or even Netlify issue (i host my website there, but they declare to support Next 15). Does anybody have an idea where the problem could be? I don't call req.socket.once anywhere in my code, so the issue is probably with one of those places
8 Replies
PinkiePie
PinkiePieOPโ€ข2mo ago
btw, the issue is only with [trpc].ts routes; the ones that uses SSR with getServerSideProps works fine. and it also works fine locally, it fails only in prod (aws lambdas backed by netlify)
Alex / KATT ๐Ÿฑ
Alex / KATT ๐Ÿฑโ€ข2mo ago
can you share the content of the /pages/api/[trpc].ts-file? is it only on prod or in development as well? is it the same when you run npm build && npm start locally? i just bumped next on our monorepo and it works fine this is the file for reference https://github.com/trpc/trpc/blob/next/examples/next-prisma-starter/src/pages/api/trpc/%5Btrpc%5D.ts you can see the deployment here
PinkiePie
PinkiePieOPโ€ข2mo ago
@Alex / KATT ๐Ÿฑ
import { createNextApiHandler } from '@trpc/server/adapters/next';
import { createContext } from '../../../../lambdas/src/trpc/context';
import { appRouter } from '../../../../lambdas/src/trpc/router/_app';

// export API handler
export default createNextApiHandler({
router: appRouter,
createContext,
onError:
process.env.NODE_ENV === 'development'
? ({ path, error }) => {
console.error(`โŒ tRPC failed on ${path}: ${error}`);
}
: undefined,
});

export const config = {
api: {
bodyParser: false,
responseLimit: '20mb',
},
};
import { createNextApiHandler } from '@trpc/server/adapters/next';
import { createContext } from '../../../../lambdas/src/trpc/context';
import { appRouter } from '../../../../lambdas/src/trpc/router/_app';

// export API handler
export default createNextApiHandler({
router: appRouter,
createContext,
onError:
process.env.NODE_ENV === 'development'
? ({ path, error }) => {
console.error(`โŒ tRPC failed on ${path}: ${error}`);
}
: undefined,
});

export const config = {
api: {
bodyParser: false,
responseLimit: '20mb',
},
};
it works locally both with dev/prod build (both yarn next dev and yarn next build && yarn next start) , the issue happens only when i deploy to netlify btw it's trpc 11.0.0-rc.666 and next 15.1.2 if it's anyhow could help
PinkiePie
PinkiePieOPโ€ข2mo ago
@Alex / KATT ๐Ÿฑ it seems like i narrowed down the issue to the exact file and commit ๐Ÿ™‚ The change was introduced in this commit/line: https://github.com/trpc/trpc/commit/87fde62978441b1cedd1d5cf7290f6ade9a92607#diff-981f2f06bfbbee188e14a4f7991f370e9042eb5c21995e24e183ab76acc5ce01R136 and then here https://github.com/trpc/trpc/commit/3df8b21beb4925cf73194fc776d1a88603a3c420#diff-981f2f06bfbbee188e14a4f7991f370e9042eb5c21995e24e183ab76acc5ce01L136 req.socket exists locally with node, but it seems like it doesn't exist in lambdas environment (next adapter with netlify backed by aws). so, it fails on this line. In this env, request object is an instance of ComputeJsIncomingMessage class - I don't really know if it comes from next itself, or from lambdas, it's what i've seen when i put console.log in trpc code using yarn patch. It looks like this issue has nothing to do with nextjs upgrade, but with the fact i didn't pin exact trpc version but used "@trpc/server": "next", and it was unintentionally upgraded together with nextjs. As a temporary workaround i reverted it back to a version before this change was introduced (11.0.0-rc.608), and now everything works fine
Alex / KATT ๐Ÿฑ
Alex / KATT ๐Ÿฑโ€ข2mo ago
could you create an issue on github with the above?
PinkiePie
PinkiePieOPโ€ข2mo ago
https://github.com/trpc/trpc/issues/6341 - however it's mostly just a copy-paste of the discussion above, i don't have an isolated project where issue can be reproduced ๐Ÿ˜ฆ
GitHub
bug: trpc fails on nextjs API route, deloyed on AWS lambda, backed ...
Provide environment information System: OS: Linux 6.8 Ubuntu 22.04.5 LTS 22.04.5 LTS (Jammy Jellyfish) CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor Memory: 16.91 GB / 31.24 GB Container: Yes Sh...
Alex / KATT ๐Ÿฑ
Alex / KATT ๐Ÿฑโ€ข2mo ago
should be fixed now! thanks for reporting!
PinkiePie
PinkiePieOPโ€ข2mo ago
thank you! โค๏ธ

Did you find this page helpful?