Category: Next.js, React and TypeScript

Next.js API endpoint returns 405 for OPTIONS method

Read this guide in Polish

The app tries to send a request from another domain, but endpoint corresponds to Method Not Allowed during preflight. The route may not provide the expected methods or the OPTIONS response does not contain the required CORS headers.

How to identify the source of the problem

Check OPTIONS request at DevTools and list of exported GET/POST/OPTIONS handlers in route.ts file. Compare the required methods, headers and origin with endpoint configuration and reverse proxy.

Compare the message from the console with the result of the production compilation and the condition after the page has been refreshed.

Repair steps

Implement OPTIONS with the corresponding Access-Control-Allow-* headers for exactly allowed domains. Keep real POST authorization and return controlled 405 for deliberately unsupported methods.

How to Test Solution

Preflight passes, and trusted origin can send the right POST, while maintaining a refusal for unauthorised customers.

Security and restrictions

Do not confuse CORS with user authorisation; the compatible domain itself does not give access to data.