How do I use the rsc-rq-prefetch example with a protected procedure?
Thank you for this implementation! It looks sooo promising 😀
However, I ran into an issue when trying it out:
https://github.com/trpc/trpc/blob/next/examples/.experimental/next-app-dir/src/app/rsc-rq-prefetch/page.tsx
If we have a protected procedure instead of a public one, the server is not authenticated during SSR and an error is thrown.
How can we solve this? Can we just ignore the error and will it still work?
GitHub
trpc/examples/.experimental/next-app-dir/src/app/rsc-rq-prefetch/pa...
🧙♀️ Move Fast and Break Nothing. End-to-end typesafe APIs made easy. - trpc/trpc
13 Replies
I think something changed between rc.403 and current RC versions. I switched from npm to pnpm today and it updated me to the latest RC and this broke. It started trying to run the client query on the server which caused my console to go nuts with a huge error output. Rolling back to 403 and it's working again.
oh my god, yep. that was the issue .... sigh, I was so confused
Is there a changelog of some kind so I know if the PR is even released in rc.403?
My guess is this is related: https://github.com/trpc/trpc/pull/5810
GitHub
fix(react-query): return same
Proxy
-object to avoid infinite recu...Closes #5808
🎯 Changes
This is a bit of a mindfuck.
✅ Checklist
I have followed the steps listed in the Contributing guide.
If necessary, I have added documentation related to the changes made.
...
Hi @testsubject1137
Have you had some success with more recent versions in the meantime? Even with rc.403, I'm getting an error in my
isAuthed
trpc middleware.
since, as you've said, the server is running the client component query during ssr.This is how I'm doing it, and it's working on rc.403.
Hmm, does your trpc client component client run during SSR?
I'm experiencing strange behaviour...
My next.js receives 3 trpc requests for the following page:
foo/page.tsx
Server Logs
rsc.ts (the RSC query client)
rcc.tsx
foo.tsx (Component with useSuspenseQuery)
So what I would expect is:
The trpc handler should be hit 1-2 times instead of 3 times.
- Once for the
void trpc.foo.prefetch();
in page.tsx
('x-trpc-source': 'rsc'
)
- Maybe once for the trpc.foo.useSuspenseQuery();
in foo.tsx
('x-trpc-source': 'react-query'
) in case the server prefetch failed.
But it should never be called during SSR with the trpc client component in rcc.tsx
('x-trpc-source': 'react-query-ssr'
).
Are those assumptions correct?Do you have all of the packages locked to 403?
I know I had some unexpected behavior when I didn't.
I have not tested to see how many/which calls tRPC is making, but my auth does work just fine. However, I don't know if it makes any difference, but mine is set up a little differently. Maybe this will help a little.
/server/api/trpc.ts
/trpc/server.ts
/trpc/react.tsx
Yes, I have
What version of tanstack react-query do you use? Maybe that's the issue and not trpc itself...
Since, I don't think trpc is deciding if it should run during SSR or not...
If you do something like this
in the
httpBatchStream
, and also log the headers in the protected procedure middleware - does it get logged? Because I think my setup is wrongfully calling the useSuspense query on the server during SSR and, since the server during rendering is somehow not authed, it fails...I can test this for you tonight.
@michaelschufi I actually just get one
x-trpc-source: rsc
callThank you! This helps a lot. Now I know that this is the issue 🙏
Let me know if there are any code samples I can share to help you narrow it down.
Oh!
"@tanstack/react-query": "^5.49.0",
i'll try upgrading soon to the newest rc anyways. because of #Can I use the "Streaming with Server Components" strategy with tRPC? seems to be working.
Note:
This has fixed itself after upgrading to a new
@tanstack/react-query
version. Only updating the rc version of tRPC was somehow insufficient 🤷♂️
Okay, this is not true. It only works if there's a prefetch going on. Probably related to https://github.com/vercel/next.js/discussions/60640
GitHub
Allow Client Components access to request headers during SSR · verc...
Goals Allow client components to make authenticated fetch requests during the SSR prepass Non-Goals Server Components, as that's already possible using the headers() function Fetch requests mad...