2024-09-09 web, development, javascript
Solving CORS Issues in Next.js 13 with API Routes
By O. Wolfson
Next.js, the open-source React framework developed by Vercel, has recently released its 13th version. As an outstanding development tool, it offers a wide range of functionalities, including API routes. However, if you've been exploring this feature, you might have come across some CORS (Cross-Origin Resource Sharing) issues. This problem generally appears when dealing with POST requests on a publicly available API. But don't worry; there's a solution to handle it efficiently.
In this article, we will delve into a method for handling CORS issues in Next.js 13 API routes, particularly for POST requests.
Setting up Middleware for CORS Handling
The first thing you need to do is set up a middleware to handle CORS at the root of your project. Middleware allows us to handle requests and responses in a way we see fit, especially when it comes to setting specific headers.
Create a new file named middleware.ts
in the root of your project and add the following code:
This middleware
function performs several important tasks:
- It retrieves the
origin
from the request headers, which is crucial for identifying the origin of the incoming request. - It creates a new response object using
NextResponse.next()
. - It sets several necessary headers on the response, such as
Access-Control-Allow-Origin
,Access-Control-Allow-Methods
,Access-Control-Allow-Headers
,Access-Control-Allow-Credentials
, andAccess-Control-Max-Age
. - Finally, it logs that the middleware has been executed and returns the updated response.
Note that the config
export sets the matcher to apply this middleware to all API routes.
Setting Up an API Route
The second step is setting up the API route itself. Next.js provides the ability to create serverless functions as API routes, and we can use this functionality to accept POST requests.
Set up your API route file (e.g., app/api/test/route.ts
), using the following structure:
The POST
function will be called whenever a POST request is made to this API route. The request body is parsed using req.json()
, and the data can be extracted and used as required.
In case of an error, we catch it, log it to the console, and return an error response using NextResponse.error()
.
Here is the request. Note that we are using the fetch
API to make the request:
Wrapping Up
By now, you should have a functioning Next.js API route that can handle POST requests without any CORS issues.