fetchQuery fails inside clerkMiddleware (clerk/nextjs)
I'll add a screenshot once I land, as I just found this issue right before boarding a flight.
It seems that fetchQuery fails (with or without token added) when called inside the new clerkMiddleware (replacing their authMiddleware).
Anyone have experience with fetchQuery inside nextjs middleware? I've checked the auth().getToken({ template: "convex" }) returning a proper token.
18 Replies
error is propagating from http_client, no error message available, only returning stack trace.
same code was functioning correctly when using authMiddleware. issue only happening after changing to clerkMiddleware.
Hmm, this sounds similar to https://discord.com/channels/1019350475847499849/1269683954772869182/1270452486888751268
If you're getting a similar message, you can find what failed on the Convex dashboard (you can search by Request ID)
Hi @Michal Srb it turns out, it was the problem related to Vercel's incident yesterday. This was why the error was not traceable from both the logs and convex dashboard.
Ok sorry, I'm back and I'm not 100% sure if this is Vercel's problem again. I've raised a ticket there as well:
Situation:
was returning an empty error in vercel log. api.foo was never triggered (I've checked this on multiple level)
so I tried using the ConvexHttpClient (set up correctly with the url and auth).
then I ended up trying native fetch to mimic the behavior of function.
- This works perfectly fine (fetchQuery, client.query, fetch) on localhost.
- Inside the Vercel Middleware however, I see 404 response with empty body (which is why the error was empty, because it is written to which is an empty string.
Tried with and . same result, works perfectly fine on localhost but not on vercel middleware.
I'm leaving this here just in case others are experiencing same on the middleware or server actions of Vercel.
funny thing is clerk requests are working fine. just the convex requests are returning 404.
adding the response from vercel.
Hi @Michal Srb seems like this issue needs to be reopened. Both myself and Vercel replicated the issue and seems it is Convex returning 404. Can't figure out what is exactly causing the 404 at the moment.
hey @BakemonoHouse, I've started trying to get a repro on our side. I'm starting with a Vercel edge function and it appears to work in both dev and when deployed to vercel:
(next 14.2.5, convex 1.14.0)
https://vercel-edge-demo-jugrhlcte-sujayakars-projects.vercel.app/hello works (after configuring
NEXT_PUBLIC_CONVEX_URL
).
I'm going to try using edge middleware next.Yup I've checked NEXT_PUBLIC_CONVEX_URL exists (in case this needs validation)
I'm on convex 1.14.0
next 14.2.3
okay! i have a repro with middleware. interesting that edge functions work but middleware doesn't.
fails at https://vercel-edge-demo-6uydre4fe-sujayakars-projects.vercel.app/secret-page
i'll look at the logs on our side
const headers = {
"Content-Type": "application/json",
"Convex-Client": "npm-1.14.0",
"Authorization":
Bearer ${token}
,
};
const response = await fetch(${process.env.NEXT_PUBLIC_CONVEX_URL!}/api/query
, {
method: "POST",
headers,
body: JSON.stringify({
path: "users:checkMe",
args: {},
format: "json"
})
});
console.info("from middleware", ${process.env.NEXT_PUBLIC_CONVEX_URL!}/api/query
, response.ok, response.status);
if (!response.ok && response.status !== 560) {
console.error(response.status, await response.text());
} else {
console.info("success", await response.json())
}
yup i went down to this level to test. only breaks in the middleware. super weirdcool, that's super helpful ^^
haha it should rule out the convexclient issues. I read through the whole node_module to test...
thanks for helping out!
okay, I've made some progress. for some reason, middleware includes the
Forwarded
header in the request (in addition to X-Forwarded-For
) while edge functions do not. I'm still digging, but including the Forwarded
header is causing a layer in our system to reject the request with a 404.should I relay this back to Vercel team for more details?
I think the issue is on our side (adding the
Forwarded
header shouldn't cause us to return 404), but it's definitely interesting. here's an example of a "bad" request on my test app that includes both Forwarded
and the X-Forwarded-*
headers:
yup I did assume so too. glad to be narrowing this down 🙂
okay, we reverted a routing change on our side, and my repro now works. @BakemonoHouse, can you see if it's fixed for you too?
let me try
yup it has been resolved
thanks a lot!
of course! thanks for your patience and working together with us on this bug.