Blog Image

Fix: Next - Fix Angular - Access to XMLHttpRequest from origin has been blocked by CORS policy:

Cross-Origin Resource Sharing (CORS) is a fundamental security feature implemented by browsers to restrict web pages from making requests to a different domain than the one that served the web page. While CORS is essential for maintaining web security, it can sometimes pose challenges during development, especially when integrating frontend applications with Next.js API routes. In this blog post, we’ll explore how to resolve CORS errors in Next.js, ensuring seamless communication between your frontend and backend. Additionally, we’ll discuss using corsproxy.io for production environments.

Table of Contents

  1. Understanding CORS
  2. CORS in Next.js API Routes
  3. Common CORS Errors
  4. Fixing CORS Errors in Next.js
  5. Using corsproxy.io for Production
  6. Best Practices
  7. Conclusion

Understanding CORS

CORS is a security mechanism enforced by browsers to control how web applications interact with resources from different origins. An “origin” comprises the protocol, domain, and port of a URL. For example, https://example.com and http://example.com are considered different origins due to the protocol difference.

When a frontend application (e.g., running on http://localhost:3000) tries to access a backend API (e.g., https://api.example.com), the browser performs a CORS check to determine if the request is allowed. If the server doesn’t explicitly permit the requesting origin, the browser blocks the request, resulting in a CORS error.

CORS in Next.js API Routes

Next.js allows developers to create API routes within the same project, facilitating seamless integration between frontend and backend. However, when the frontend and API routes are hosted on different origins (e.g., frontend on https://www.myapp.com and API on https://api.myapp.com), CORS policies come into play.

By default, Next.js API routes do not include CORS headers, meaning cross-origin requests will be blocked by browsers unless explicitly allowed.

Common CORS Errors

Developers often encounter the following CORS-related errors:

  1. No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

    • This indicates that the server hasn’t included the Access-Control-Allow-Origin header in its response, making the browser block the request.
  2. The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ’*’ when the request’s credentials mode is ‘include’.

    • This error occurs when the server allows any origin (*) but the frontend is making requests with credentials (e.g., cookies, HTTP authentication).
  3. Request header field [Header-Name] is not allowed by Access-Control-Allow-Headers.

    • This happens when the frontend sends custom headers not permitted by the server’s CORS policy.

Fixing CORS Errors in Next.js

Resolving CORS issues involves configuring your Next.js API routes to include the appropriate CORS headers. Here are several methods to achieve this:

1. Setting CORS Headers in API Routes

One straightforward approach is to manually set the necessary CORS headers in each API route. Here’s how you can do it:

// pages/api/hello.js

export default function handler(req, res) {
  // Allow requests from specific origins
  res.setHeader('Access-Control-Allow-Origin', 'https://www.yourfrontend.com');
  
  // Specify allowed HTTP methods
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  
  // Specify allowed headers
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  
  // Handle preflight OPTIONS request
  if (req.method === 'OPTIONS') {
    res.status(200).end();
    return;
  }

  // Your API logic here
  res.status(200).json({ message: 'Hello from Next.js API!' });
}

Explanation:

2. Using Middleware for CORS

If you have multiple API routes, manually setting headers in each can be repetitive. Instead, you can use middleware to apply CORS headers globally.

Using nextjs-cors Package

The nextjs-cors package simplifies CORS handling in Next.js.

Installation:

npm install nextjs-cors

Usage:

// pages/api/hello.js

import NextCors from 'nextjs-cors';

export default async function handler(req, res) {
  // Run the cors middleware
  await NextCors(req, res, {
    // Options
    methods: ['GET', 'POST', 'OPTIONS'],
    origin: 'https://www.yourfrontend.com',
    optionsSuccessStatus: 200, // Some legacy browsers choke on 204
  });

  // Your API logic here
  res.status(200).json({ message: 'Hello from Next.js API with CORS!' });
}

Explanation:

Creating a Custom Middleware

Alternatively, you can create a custom middleware to apply CORS headers across all API routes.

Creating the Middleware:

// middleware/cors.js

export default function cors(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', 'https://www.yourfrontend.com');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');

  if (req.method === 'OPTIONS') {
    res.status(200).end();
    return;
  }

  next();
}

Applying the Middleware:

// pages/api/hello.js

import cors from '../../middleware/cors';

export default function handler(req, res) {
  cors(req, res, () => {
    // Your API logic here
    res.status(200).json({ message: 'Hello from Next.js API with custom CORS middleware!' });
  });
}

Note: While custom middleware provides flexibility, using established packages like nextjs-cors can save time and reduce potential errors.

3. Leveraging CORS Libraries

There are several libraries available that simplify CORS configuration in Next.js. One popular choice is cors, a widely-used Node.js package.

Installation:

npm install cors

Usage:

// pages/api/hello.js

import Cors from 'cors';

// Initialize the cors middleware
const cors = Cors({
  methods: ['GET', 'POST', 'OPTIONS'],
  origin: 'https://www.yourfrontend.com',
});

// Helper method to wait for a middleware to execute before continuing
function runMiddleware(req, res, fn) {
  return new Promise((resolve, reject) => {
    fn(req, res, (result) => {
      if (result instanceof Error) {
        return reject(result);
      }
      return resolve(result);
    });
  });
}

export default async function handler(req, res) {
  // Run the middleware
  await runMiddleware(req, res, cors);

  // Your API logic here
  res.status(200).json({ message: 'Hello from Next.js API with cors package!' });
}

Explanation:

Pros of Using Libraries:

Using services like corsproxy.io

While configuring CORS headers on your server is the recommended approach, there are scenarios where you might not have control over the backend server, or you need a quick solution for specific use cases. In such cases, using a CORS proxy like corsproxy.io can be beneficial.

What is corsproxy.io?

corsproxy.io is a simple proxy server that adds CORS headers to your requests, allowing your frontend application to access resources from servers that do not include the necessary CORS headers.

How to Use corsproxy.io:

Instead of making a request directly to your Next.js API route, prepend the URL with the corsproxy.io proxy URL.

Example:

const proxyUrl = 'https://corsproxy.io/?url=';
const apiUrl = 'https://api.yourdomain.com/api/hello';
const fullUrl = `${proxyUrl}${encodeURIComponent(apiUrl)}`;

fetch(fullUrl)
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error('Error:', error));

Considerations for Production:

  1. Performance: Introducing a proxy can add latency to your requests. Ensure that the proxy service you choose is reliable and has low latency.

  2. Security: Be cautious when proxying sensitive data. Ensure that the proxy service is trustworthy and that data is transmitted securely.

  3. Rate Limits: Free proxy services may impose rate limits. For high-traffic applications, consider using a dedicated proxy service or implementing your own proxy.

  4. Cost: Some proxy services charge based on usage. Evaluate the cost implications based on your application’s needs.

Why Use corsproxy.io:

Create a free Account to fix CORS Errors in Production

Say goodbye to CORS errors and get back to building great web applications. It's free!

App screenshot