Convex Auth Middleware Works in Dev, Fails in Production with headers Error
Hi, I'm trying to get my web app to production with nextjs, bun, convex but I've encountered this runtime error in logs.
Error:
headers
was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
I think this caused by nextjs middleware.
What I've Confirmed:
My CONVEX_URL
and CONVEX_DEPLOYMENT_KEY
environment variables are set correctly in my production environment's settings.
I have run npx convex deploy and the production site is configured to use the production Convex URL.
The code uses the modern ConvexAuth pattern as recommended in the docs.
here's my middleware code if you're wondering
next:15.2.3
convex:^1.23.0
@convex-dev/auth:^0.0.81
4 Replies
Everything in your middleware file is set up correctly, I'd grep the codebase for "headers" and see where else it might be called.
I cannot find anything that is related to "headers"
I still can't really find or solve the problem sadly
but i notice that the error hasn't occur when I'm building and running my application on my local machine, instead it only happen when I'm using docker to deploy my application
I've tried many things like downgrade convex version, downgrade nextjs version, remove middleware entirely (which make my app unusable). and none of it works
That’s really odd, all I can think is your docker deployed code is somehow different than expected - headers is being called somewhere, can’t really get this error otherwise
same here, and it only appear to happen with only docker for some reason
works fine when deployed with podman