CORS problem
Answered
Bombay-duck posted this in #help-forum
Bombay-duckOP
Hi,
I have problem with CORS that I can't handle, please help if possible.
["next": "^13.4.19",]
I have a middleware:
in route I return headers:
but I still get:
looking forward to your help, I have no idea what else to do
Piotr
I have problem with CORS that I can't handle, please help if possible.
["next": "^13.4.19",]
I have a middleware:
import { NextResponse } from 'next/server;
const allowedOrigins =
process.env.NODE_ENV === 'production'
? ['https://www.art-ck.pl', 'https://art-ck.pl']
: ['http://localhost:3000'];
export function middleware(request: Request) {
const origin = request.headers.get('origin');
console.log(origin);
if (origin && !allowedOrigins.includes(origin)) {
return new NextResponse(null, {
status: 400,
statusText: 'Bad request',
headers: {
'Content-Type': 'text/plain',
},
});
}
return NextResponse.next();
}
export const config = {
matcher: '/api/:path*',
};in route I return headers:
export async function GET(req: NextRequest, _res: NextResponse) {
const origin = req.headers.get('origin');
return new NextResponse(JSON.stringify({ message: 'text message' }), {
status: 200,
headers: {
'Access-Control-Allow-Origin': origin || '*',
'Content-Type': 'application/json',
},
});
}but I still get:
Access to fetch at 'https://art-ck.pl/api/newsletter' from origin 'https://www.art-ck.pl' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
588-322a5451a4ef4b8c.js:1
POST https://art-ck.pl/api/newsletter net::ERR_FAILEDlooking forward to your help, I have no idea what else to do
Piotr
6 Replies
Try this maybe:
https://stackoverflow.com/a/42172732/8025414
https://stackoverflow.com/a/42172732/8025414
Answer
Bombay-duckOP
jop. That was the case. Thank you very much for the tip!!! ðŸ‘
Cape lion
It didn't work for me
but thanks!
Cape lion
actually, we are moving from vercel to explore other options!
Bombay-duckOP
I tried Railway - quite a nice service. But I also came across problems with domain. They needed some DNS records that my host provider was not capable of provide. I didn't want to have dozen of places for different domains to handle ... so I came back to Vercel 🙂