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

Docs: CORS Middleware example uses deprecated/non-working NextResponse.json() #70406

Open
alexandros-megas opened this issue Sep 24, 2024 · 1 comment
Labels
Documentation Related to Next.js' official documentation.

Comments

@alexandros-megas
Copy link

What is the documentation issue?

The page for Middleware demonstrates how to use middleware to implement CORS, setting the appropriate headers for OPTIONS requests, and also adding CORS headers to other responses.

return NextResponse.json({}, { headers: preflightHeaders })

If you run this example, you will get the following error when making an OPTIONS request:

[Error: A middleware can not alter response's body. Learn more: https://nextjs.org/docs/messages/returning-response-body-in-middleware]

This page tells you that due to this being an anti-pattern, it's not allowed to create your own response body in middleware, and instead you should redirect/rewrite.

I was able to work around this, by creating my own endpoint, pages/api/cors-preflight.ts with the following:

export default async function handlePreflight (_req, res) {
  res.setHeader('Access-Control-Allow-Credentials', 'true');
  res.setHeader('Access-Control-Allow-Origin', 'http://mydomain.com');
  res.setHeader('Access-Control-Allow-Methods', 'GET,DELETE,PATCH,POST,PUT');
  res.setHeader('Access-Control-Allow-Headers', 'header-1, header-2, ...');
  res.status(200).end();
}

and then replacing NextResponse.json(...) with the following 2 lines:

    const nextUrl = `${allowedOrigin}/api/cors-preflight`;
    return NextResponse.rewrite(nextUrl, { headers: corsOptions });

Is there any context that might help us understand?

I think the issue is pretty thoroughly explained. The docs should be updated to reflect the correct use of middleware.

Does the docs page already exist? Please link to it.

https://nextjs.org/docs/pages/building-your-application/routing/middleware#cors

@alexandros-megas alexandros-megas added the Documentation Related to Next.js' official documentation. label Sep 24, 2024
@samcx
Copy link
Member

samcx commented Sep 24, 2024

@alexandros-megas I'm not getting this error when copy-pasting the snippet from the docs. To clarify, NextResponse.json is not drepecated.

Can you create a :repro: so we can take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Related to Next.js' official documentation.
Projects
None yet
Development

No branches or pull requests

2 participants