Sthap
Sthap3mo ago

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_KEYenvironment 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
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
nextjsMiddlewareRedirect,
} from "@convex-dev/auth/nextjs/server";

const isSignInPage = createRouteMatcher(["/signin"]);
const isProtectedRoute = createRouteMatcher(["/", "/server", "/dashboard(.*)"]);

export default convexAuthNextjsMiddleware(async (request, { convexAuth }) => {
if (isSignInPage(request) && (await convexAuth.isAuthenticated())) {
return nextjsMiddlewareRedirect(request, "/");
}
if (isProtectedRoute(request) && !(await convexAuth.isAuthenticated())) {
return nextjsMiddlewareRedirect(request, "/signin");
}
});

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
}
import {
convexAuthNextjsMiddleware,
createRouteMatcher,
nextjsMiddlewareRedirect,
} from "@convex-dev/auth/nextjs/server";

const isSignInPage = createRouteMatcher(["/signin"]);
const isProtectedRoute = createRouteMatcher(["/", "/server", "/dashboard(.*)"]);

export default convexAuthNextjsMiddleware(async (request, { convexAuth }) => {
if (isSignInPage(request) && (await convexAuth.isAuthenticated())) {
return nextjsMiddlewareRedirect(request, "/");
}
if (isProtectedRoute(request) && !(await convexAuth.isAuthenticated())) {
return nextjsMiddlewareRedirect(request, "/signin");
}
});

export const config = {
matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
}
next:15.2.3 convex:^1.23.0 @convex-dev/auth:^0.0.81
No description
4 Replies
erquhart
erquhart2mo ago
Everything in your middleware file is set up correctly, I'd grep the codebase for "headers" and see where else it might be called.
Sthap
SthapOP2mo ago
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
erquhart
erquhart2mo ago
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
Sthap
SthapOP2mo ago
same here, and it only appear to happen with only docker for some reason works fine when deployed with podman

Did you find this page helpful?