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

Use of headers() in next 15 give a error #13

Open
ncasola opened this issue Oct 24, 2024 · 2 comments
Open

Use of headers() in next 15 give a error #13

ncasola opened this issue Oct 24, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@ncasola
Copy link

ncasola commented Oct 24, 2024

Describe the bug

Use of headers() in next 15 give a error:
[ Server ] Error: Route "/private" used headers().get('X-FiefAuth-Access-Token-Info'). headers() should be awaited before using its value. Learn more: https://nextjs.org/docs/messages/sync-dynamic-apis

To Reproduce

Follow the integrate guide in https://docs.fief.dev/integrate/javascript/ssr/nextjs-app/

Configuration

  • JavaScript environment: NextJs 15, pnpm, TS

  • Fief client version: npm @/fief latest

  • Fief Cloud or self-hosted: self-hosted

  • If self-hosted, Fief version: ghcr.io/fief-dev/fief:latest

Additional context

image

@ncasola ncasola added the bug Something isn't working label Oct 24, 2024
@frankie567
Copy link
Member

Thank you for the report @ncasola 🙏

This is a breaking change introduced in Next.js 15... It's really hard to keep up with them, this is exhausting.

@ysnbyzli
Copy link

Thank you for the report @ncasola 🙏

This is a breaking change introduced in Next.js 15... It's really hard to keep up with them, this is exhausting.

Hello, I would like to contribute to this issue. Could you guide me on how to proceed?

After reviewing the code, I believe the issue can be resolved by following these steps:
• The function should be converted to an async function, and the const headersList = headers(); line should be awaited.
• The places where the function is used should also be updated to use async/await.

Here is a proposed solution example:

const getServerSideHeaders = async (headerName: string, req?: IncomingMessage): Promise<string | null> => {
  // "Legacy" Next.js, req object is required
  if (req) {
    return req.headers[headerName.toLowerCase()] as string | null;
  }
  // Next.js 15 style
  const headersList = await headers();
  return headersList.get(headerName);
};

This way, the headers() function will be handled asynchronously, and values will be retrieved correctly. Could you assist me with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants