Unverified Commit 57dcdd90 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Bypass OPTIONS handling for API routes (#12978)

parent 32057c84
...@@ -62,6 +62,7 @@ ...@@ -62,6 +62,7 @@
"cheerio": "0.22.0", "cheerio": "0.22.0",
"clone": "2.1.2", "clone": "2.1.2",
"cookie": "0.4.1", "cookie": "0.4.1",
"cors": "2.8.5",
"coveralls": "3.0.3", "coveralls": "3.0.3",
"cross-env": "6.0.3", "cross-env": "6.0.3",
"cross-spawn": "6.0.5", "cross-spawn": "6.0.5",
......
...@@ -9,7 +9,7 @@ import webpack from 'webpack' ...@@ -9,7 +9,7 @@ import webpack from 'webpack'
import { createEntrypoints, createPagesMapping } from '../build/entries' import { createEntrypoints, createPagesMapping } from '../build/entries'
import { watchCompilers } from '../build/output' import { watchCompilers } from '../build/output'
import getBaseWebpackConfig from '../build/webpack-config' import getBaseWebpackConfig from '../build/webpack-config'
import { NEXT_PROJECT_ROOT_DIST_CLIENT } from '../lib/constants' import { NEXT_PROJECT_ROOT_DIST_CLIENT, API_ROUTE } from '../lib/constants'
import { recursiveDelete } from '../lib/recursive-delete' import { recursiveDelete } from '../lib/recursive-delete'
import { import {
BLOCKED_PAGES, BLOCKED_PAGES,
...@@ -46,6 +46,12 @@ export async function renderScriptError(res: ServerResponse, error: Error) { ...@@ -46,6 +46,12 @@ export async function renderScriptError(res: ServerResponse, error: Error) {
} }
function addCorsSupport(req: IncomingMessage, res: ServerResponse) { function addCorsSupport(req: IncomingMessage, res: ServerResponse) {
const isApiRoute = req.url!.match(API_ROUTE)
// API routes handle their own CORS headers
if (isApiRoute) {
return { preflight: false }
}
if (!req.headers.origin) { if (!req.headers.origin) {
return { preflight: false } return { preflight: false }
} }
......
import Cors from 'cors'
// Helper method to wait for a middleware to execute before continuing
// And to throw an error when an error happens in a middleware
function initMiddleware(middleware) {
return (req, res) =>
new Promise((resolve, reject) => {
middleware(req, res, result => {
if (result instanceof Error) {
return reject(result)
}
return resolve(result)
})
})
}
// Initialize the cors middleware
const cors = initMiddleware(
// You can read more about the available options here: https://github.com/expressjs/cors#configuration-options
Cors({
// Only allow requests with GET, POST and OPTIONS
methods: ['GET', 'POST', 'OPTIONS'],
})
)
export default async function handler(req, res) {
// Run cors
await cors(req, res)
// Rest of the API logic
res.json({ message: 'Hello Everyone!' })
}
...@@ -44,6 +44,20 @@ function runTests(dev = false) { ...@@ -44,6 +44,20 @@ function runTests(dev = false) {
expect(res.status).not.toEqual(404) expect(res.status).not.toEqual(404)
}) })
it('should set cors headers when adding cors middleware', async () => {
const res = await fetchViaHTTP(appPort, '/api/cors', null, {
method: 'OPTIONS',
headers: {
origin: 'example.com',
},
})
expect(res.status).toEqual(204)
expect(res.headers.get('access-control-allow-methods')).toEqual(
'GET,POST,OPTIONS'
)
})
it('should work with index api', async () => { it('should work with index api', async () => {
const text = await fetchViaHTTP(appPort, '/api', null, {}).then( const text = await fetchViaHTTP(appPort, '/api', null, {}).then(
res => res.ok && res.text() res => res.ok && res.text()
......
...@@ -5796,6 +5796,14 @@ core-util-is@1.0.2, core-util-is@~1.0.0: ...@@ -5796,6 +5796,14 @@ core-util-is@1.0.2, core-util-is@~1.0.0:
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7" resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac= integrity sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=
cors@2.8.5:
version "2.8.5"
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
dependencies:
object-assign "^4"
vary "^1"
cosmiconfig@^2.1.0, cosmiconfig@^2.1.1: cosmiconfig@^2.1.0, cosmiconfig@^2.1.1:
version "2.2.2" version "2.2.2"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-2.2.2.tgz#6173cebd56fac042c1f4390edf7af6c07c7cb892" resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-2.2.2.tgz#6173cebd56fac042c1f4390edf7af6c07c7cb892"
...@@ -12568,7 +12576,7 @@ oauth-sign@~0.9.0: ...@@ -12568,7 +12576,7 @@ oauth-sign@~0.9.0:
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455" resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ== integrity sha512-fexhUFFPTGV8ybAtSIGbV6gOkSv8UtRbDBnAyLQw4QPKkgNlsH2ByPGtMUqdWkos6YCRmAqViwgZrJc/mRDzZQ==
object-assign@4.1.1, object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1: object-assign@4.1.1, object-assign@^4, object-assign@^4.0.1, object-assign@^4.1.0, object-assign@^4.1.1:
version "4.1.1" version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863" resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM= integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
...@@ -18064,7 +18072,7 @@ validate-npm-package-name@3.0.0, validate-npm-package-name@^3.0.0: ...@@ -18064,7 +18072,7 @@ validate-npm-package-name@3.0.0, validate-npm-package-name@^3.0.0:
dependencies: dependencies:
builtins "^1.0.3" builtins "^1.0.3"
vary@~1.1.2: vary@^1, vary@~1.1.2:
version "1.1.2" version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw= integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment