Convex+Clerk: sign out unhandled runtime error
Hi Convex team, I've been battling this issue for a couple of weeks and have no idea what to try anymore.
I have the latest versions of everything: Clerk, Convex, Nextjs
For Convex, I combine RSC and client components, using fetchQuery for RSC and of course useQuery and useMutation on the client.
I setup everything according to both, Clerk and Convex documentation
in the middleware.ts I have:
in my authed layout I have:
And I return
null
for all queries, when ctx.auth.getUserIdentity() is null.
The problem happens when I logout, the app just crashes with an "unhandled runtime error"
This is also happening on production22 Replies
Thanks @Khalil can you:
1. Include the full error stack trace (it looks minimized maybe, can you get a better one from dev maybe?)
2. Can you strip your app down and share it on GitHub? (if it's easy and feasible)
3. Check the Network tab, are there any failed requests when you click sign out?
there is an unauthorized request (401) in the network, confirming it's client-side
Can you strip your app down and share it on GitHub? (if it's easy and feasible)I could if necessary, but will probably have it in a few hours or later this week
So definitely what's happening is that the token is being fetched even though the client is already logged out
What does your
ConvexProviderWithClerk
callsite looks like? Standard?
(paste the whole file including imports please)That looks fine.
Here's what I don't understand: The stack trace suggests that the error is thrown from
ConvexProviderWithClerk
, where we call getToken
. But the call to getToken
is wrapped in a try catch. So when Clerk throws there it should not be an uncaught error.btw it's reproducible right now on prod if it helps
fastmind
Fastmind is a platform for building and deploying AI chatbots.
signup and logout
not sure if that is helpful, I also enable convex debug logs, but could not find anything wrong there
My guess is that the error is not the problem. Instead the issue is with navigating to sign-in after the sign out
I did notice that on sign out, the current page is refetched, for whatever reason
You have
<RedirectToSignIn />
in your code snippet, that's what redirects there
The question is why doesn't the sign in page render. But that seems to be some RSC issueSome layout is rendering, but then no page
that is after signin or signout?
that <div> matches the /sign-in clerk page
Yeah, so after sign out, there is a redirect to the signin page, which seems to be server rendered and only returned via the RSC payload. But somehow the page doesn't show up.
The error is definitely not uncaught, because I tried pausing on uncaught exceptions in the source panel, but did not get paused on sign out
Try to redirect to sign on a button click somewhere while still signed in/out, see if it works correctly. It could be a bug in Next.js or Clerk
is there an example repo using convex + clerk core 2 that I can refer to?
This is one I'm just working on:
https://github.com/get-convex/turbo-expo-nextjs-clerk-convex-monorepo/tree/srb/upgrade-to-latest
(But it's a monorepo setup)
GitHub
GitHub - get-convex/turbo-expo-nextjs-clerk-convex-monorepo at srb/...
Monorepo template with Turborepo, Next.js, Expo, Clerk, Convex - GitHub - get-convex/turbo-expo-nextjs-clerk-convex-monorepo at srb/upgrade-to-latest
Also what happens when you remove
ConvexProviderWithClerk
(and any code that depends on it). Then you can sign in and sign out to see if it's actually Convex related (I expect not).managed to fix it!
now there is another issue, but it seems Clerk related
the problem was this:
The
const convex = new ConvexReactClient(env.NEXT_PUBLIC_CONVEX_URL);
declaration should be outside of a React component for whatever reasonAh nice! That makes sense, you get a new Convex client every time the component renders otherwise. Thanks for sharing the fix!
(I should have spotted that earlier, my bad!)