Giter VIP home page Giter VIP logo

nextjs-edge-cors's Introduction

CORS for Next.js Edge Runtime

Easily add CORS to your Edge endpoints.

Usage

The easiest way to enable CORS is using Middleware:

// src/middleware.ts

import { CorsMiddleware } from "nextjs-edge-cors";

// Allow all origins.
export const middleware = CorsMiddleware({
  origin: "*"
})

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

Alternatively, you can apply the headers manually in your Next.js config:

// 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" },
      ]
    }
  ];
};

Advanced

You can wrap existing Middleware and/or pass CORS options to CorsMiddleware:

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

export const middleware = CorsMiddleware({
  origin: "https://www.google.com",
  // Other CORS options.
  maxAge: 123456,
  // Additional middleware.
  middleware: () => {
    return NextResponse.next({
      headers: {
        "Test-Stacked-Middleware": "PASS"
      }
    });
  }
});

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.