Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: headers was called outside a request scope. #120

Open
BayuPrasetyaUtomo opened this issue Nov 13, 2024 · 0 comments
Open

Error: headers was called outside a request scope. #120

BayuPrasetyaUtomo opened this issue Nov 13, 2024 · 0 comments

Comments

@BayuPrasetyaUtomo
Copy link

BayuPrasetyaUtomo commented Nov 13, 2024

The issue appears when using SSA in Next JS

Runtime:

Bun version 1.1.34

Package version:

  "convex": "^1.17.0"
  "@auth/core": "0.36.0"
  "@convex-dev/auth": "^0.0.74"
  "next": "^14.2.17"
  "react": "^18.3.1"
  "react-dom": "^18.3.1"

Error message:

 ✓ Compiled middleware in 10ms
 ✓ Compiled in 5ms
 ⨯ 37 |     return f;
38 | }
39 | function getServerError(error, type) {
40 |     let n;
41 |     try {
42 |         throw new Error(error.message);
                       ^
error: `headers` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context

The issue started when adding these lines to the middleware:

// middleware.ts
import { convexAuthNextjsMiddleware } from "@convex-dev/auth/nextjs/server";
 
export default convexAuthNextjsMiddleware();
 
export const config = {
  // The following matcher runs middleware on all routes
  // except static assets.
  matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};

The issue dissapear as soon as the convexAuthNextjsMiddleware was removed from the middleware and changed to Next js default middleware.

// middleware.ts
const middleware = () => {

}

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

export default middleware;

Minimum folder structure

node_modules .next and public folder has been excluded

  📦project
 ┣ 📂convex
 ┃ ┣ 📂_generated
 ┃ ┃ ┣ 📜api.d.ts
 ┃ ┃ ┣ 📜api.js
 ┃ ┃ ┣ 📜dataModel.d.ts
 ┃ ┃ ┣ 📜server.d.ts
 ┃ ┃ ┗ 📜server.js
 ┃ ┣ 📜auth.config.ts
 ┃ ┣ 📜auth.ts
 ┃ ┣ 📜http.ts
 ┃ ┣ 📜schema.ts
 ┃ ┗ 📜tsconfig.json
 ┣ 📂src
 ┃ ┣ 📂app
 ┃ ┃ ┣ 📜layout.tsx
 ┃ ┃ ┗ 📜page.tsx
 ┃ ┣ 📂components
 ┃ ┃ ┗ 📂provider
 ┃ ┃ ┃ ┗ 📜auth-client.tsx
 ┃ ┣ 📂styles
 ┃ ┃ ┗ 📜globals.css
 ┃ ┗ 📜middleware.ts
 ┣ 📜.env.local
 ┣ 📜.eslintrc.cjs
 ┣ 📜.gitignore
 ┣ 📜README.md
 ┣ 📜bun.lockb
 ┣ 📜next-env.d.ts
 ┣ 📜next.config.js
 ┣ 📜package.json
 ┣ 📜postcss.config.js
 ┣ 📜prettier.config.js
 ┣ 📜tailwind.config.ts
 ┗ 📜tsconfig.json

Layout

// layout.tsx
import "@/styles/globals.css";
import { ConvexClientProvider } from "@/components/provider/auth-client";

export default function RootLayout({
    children,
  }: Readonly<{ children: React.ReactNode }>) {
    return (
        <html lang="en">
          <body>
            <ConvexClientProvider>{children}</ConvexClientProvider>
          </body>
        </html>
    );
  }

Home

// src/app/page.tsx
"use client";
import { useAuthActions } from "@convex-dev/auth/react";
import { Authenticated, AuthLoading, Unauthenticated } from "convex/react";

export default function HomePage() {
  const { signIn, signOut } = useAuthActions();
  return (
    <main className="flex min-h-screen flex-col items-center justify-center bg-gradient-to-b from-[#2e026d] to-[#15162c] text-white">
      <AuthLoading>Loading...</AuthLoading>

      <Unauthenticated>
        <button onClick={() => void signIn("github")}>Sign in</button>
      </Unauthenticated>
      <Authenticated>
        Hello There!
        <button onClick={() => void signOut()}>Sign out</button>
      </Authenticated>
    </main>
  );
}
</details>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant