Can't do server side data fetching with RSC.
Whenever I try to use a proxy client to fetch data in a React Server Component with Nextjs14, I get an error
error: TRPCClientError: Unexpected token < in JSON at position 0
Likely trpc is returning HTML though I don't know why. Now if instead of fetching with a RSC, I trigger the data fetching via a "server action" I get the following error
The wierd thing is that when using client side fetching, via react query, everything works.
I've looked at issues, docs, gpt but I can't seam to fix this error. I've looked at similar implementations like create-t3-app
but nothing works so far. Not sure what I'm doing wrong. Some direction would be greatly appreciated.
Here's a link to a repo that recreates this issue. https://github.com/esanchezvz/trpc-nextjs-express-turborepo2 Replies
Im doing something similar here: check it out. https://github.com/lwears/ReelScore
try this
also maybe 'use server' at the top of /web/src/app/trpc/server.ts
Thanks, got it working. Still don't know what caused the issue. But the fix was to move the trpc api url to an env variable, as importing the
getBaseUrl
and passing it as part of the string to url
option for httpBatchLink
and adding the 'use server' directive to it or /web/src/app/trpc/server.ts didn't work. But env variables works and is a better approach in imo.
Thanks man!