Next-auth Session is null on initial render with useSuspenseQuery
This one's been driving me crazy and I would love some help. I can't even work out whether it's TRPC, react query, or next-auth that's causing the issue.
I'm using TRPC 11.0.0-rc-682, next-auth 5.0.0-beta.25, and the nextjs 15.1.3 app router.
I have a route with an id param:
ArtefactView
is a client component with a useSuspenseQuery
:
artefacts.getById
is protected by middleware that checks for a session and throws an unauthorized error if missing. Navigating to this page from another page works fine, but if I perform a hard refresh I get an unauthorized error and a fall-back to client rendering. I've traced the cause down to the session being null in my protectedProcedure
, but only on the initial SSR pass.
I'm sure I've messed up the setup code somewhere, but I'm not sure where to look? Any advice is greatly appreciated. I wanted to paste more code here but the character limit won't let me.Solution:Jump to solution
```typescript
// server component
export function ServerComponent() {
void trpc.procedure.prefetch()
...
2 Replies
Sorry, I missed that @conlonj25 posted basically the exact same issue a few days ago, but they didn't share a solution. I've tried wrapping the ArtefactView in a suspense container (inside page.tsx), but that didn't help.
Update: I needed a suspense wrapper. I tried that previously, but there was another query deeper in the tree that also needed a prefetch.
You have to have this structure:
Solution
And you have to prefetch every single suspense query, don't miss any