#"header was called outside of scope request error".

3 messages · Page 1 of 1 (latest)

long cosmos
#

I'm currently dealing with middleware issue of "header was called outside of scope request". Not sure why this error occured, here is the middleware setup:

import {
convexAuthNextjsMiddleware,
createRouteMatcher,
isAuthenticatedNextjs,
nextjsMiddlewareRedirect,
} from "@convex-dev/auth/nextjs/server";

const isPublicPage = createRouteMatcher(["/auth"]);

export default convexAuthNextjsMiddleware((request) => {
if (!isPublicPage(request) && !isAuthenticatedNextjs()) {
return nextjsMiddlewareRedirect(request, "/auth");
}

if (isPublicPage(request) && isAuthenticatedNextjs()) {
    return nextjsMiddlewareRedirect(request, "/");
}
//todo: redirect user from auth if logged in

});

export const config = {
// The following matcher runs middleware on all routes
// except static assets.
matcher: ["/((?!.\..|_next).)", "/", "/(api|trpc)(.)"],
};

here is the error log via nextjs:
Error: headers was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context

and i also have problem when trying to start the convex server.

✖ TypeScript typecheck via tsc failed.
To ignore failing typecheck, use --typecheck=disable.
error: Script not found "node_modules\typescript\bin\tsc"

i know i can be fixed by using --typecheck=disable. But i wanted to know what and where is the cause of this occured. Because last time, all of these errors is not present.

paper vaultBOT
#

Hi @long cosmos. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The error message "headers was called outside a request scope" is a Next.js error that typically occurs when you're trying to use a Next.js API route feature outside of an API route. This error is not directly related to Convex, but rather to how you're using Next.js. You might want to check your code to ensure you're not trying to use Next.js API route features outside of an API route. For more information, you can refer to the Next.js documentation (https://nextjs.org/docs/messages/next-dynamic-api-wrong-context).

As for the TypeScript error, it seems like the TypeScript compiler (tsc) is not found. This could be due to several reasons such as not having TypeScript installed in your project or having a misconfigured tsconfig.json file. You can try installing TypeScript in your project by running npm install --save-dev typescript or yarn add --dev typescript if you're using Yarn. If TypeScript is already installed, you might want to check your tsconfig.json file for any misconfigurations.

Unfortunately, without more information, it's hard to pinpoint the exact cause of these issues. If these issues persist, you might want to seek help from the Next.js and TypeScript communities or check their respective documentations for more information.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: