Skip to content

How to properly implement CORs for API routes - App Directory + Middleware #52933

Answered by jacobawebb
jacobawebb asked this question in Help
Discussion options

You must be logged in to vote

@icyJoseph helped point out what the issue was. To summarise with an example:

My endpoint being requested only had a PUT handler, because that's all I needed at the time.

Because preflight will make an OPTIONS request to the same endpoint, if there's no OPTIONS handler then CORs fails.

Before:

export const PUT = async (request: NextRequest) => {
    // Handle PUT request here
}

After:

export const PUT = async (request: NextRequest) => {
    // Handle PUT request here
}

export const OPTIONS = async (request: NextRequest) => {
  return new NextResponse('', {
    status: 200
  })
}

I wasn't sure what to put for the body of the OPTIONS handler but the answer serves its purpose.

Replies: 2 comments 13 replies

Comment options

You must be logged in to vote
7 replies
@jacobawebb
Comment options

@icyJoseph
Comment options

@jacobawebb
Comment options

@icyJoseph
Comment options

@jacobawebb
Comment options

Comment options

You must be logged in to vote
6 replies
@IhsenBouallegue
Comment options

@nicope
Comment options

@DplAzim
Comment options

@Mifayo
Comment options

@Mifayo
Comment options

Answer selected by jacobawebb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
7 participants