📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP
Socket
Sign inDemoInstall
Socket

nextjs-edge-cors

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nextjs-edge-cors

The easiest way to enable CORS is in your Next config, per the [docs](https://vercel.com/guides/how-to-enable-cors):

1.0.0
latest
npm
Version published
Weekly downloads
11
120%
Maintainers
1
Weekly downloads
 
Created
Source

Enable CORS for Next.js Edge

The easiest way to enable CORS is in your Next config, per the docs:

// next.config.mjs

export const headers = () => {
  return [
    {
      source: "/api/cors/:path*",
      headers: [
        { key: "Access-Control-Allow-Credentials", value: "true" },
        { key: "Access-Control-Allow-Origin", value: "*" },
        { key: "Access-Control-Allow-Methods", value: "GET,OPTIONS,PATCH,DELETE,POST,PUT" },
        { key: "Access-Control-Allow-Headers", value: "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version" },
      ]
    }
  ];
};

Alternatively, you can use the withCors() Middleware wrapper:

// src/middleware.ts
import { withCors } from "nextjs-edge-cors"

export const middleware = withCors();

export const config = {
  matcher: ["/api/cors/:path*"]
};

Advanced

You can wrap existing Middleware or pass CORS options to withCors():

import { withCors } from "nextjs-edge-cors"

export const middleware = withCors(
  otherMiddleware,
  {
    /**
     * Only allow CORS requests from Google.com. Comma separate for multiple
     * values.
     */
    origin: "https://www.google.com"
  }
);

export const config = {
  matcher: ["/api/cors/:path*"]
};

FAQs

Package last updated on 28 Jul 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts