Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
57dcdd90
Unverified
Commit
57dcdd90
authored
May 16, 2020
by
Tim Neutkens
Committed by
GitHub
May 16, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Bypass OPTIONS handling for API routes (#12978)
parent
32057c84
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
64 additions
and
3 deletions
+64
-3
package.json
package.json
+1
-0
hot-reloader.ts
packages/next/server/hot-reloader.ts
+7
-1
cors.js
test/integration/api-support/pages/api/cors.js
+32
-0
index.test.js
test/integration/api-support/test/index.test.js
+14
-0
yarn.lock
yarn.lock
+10
-2
No files found.
package.json
View file @
57dcdd90
...
...
@@ -62,6 +62,7 @@
"
cheerio
"
:
"0.22.0"
,
"
clone
"
:
"2.1.2"
,
"
cookie
"
:
"0.4.1"
,
"
cors
"
:
"2.8.5"
,
"
coveralls
"
:
"3.0.3"
,
"
cross-env
"
:
"6.0.3"
,
"
cross-spawn
"
:
"6.0.5"
,
...
...
packages/next/server/hot-reloader.ts
View file @
57dcdd90
...
...
@@ -9,7 +9,7 @@ import webpack from 'webpack'
import
{
createEntrypoints
,
createPagesMapping
}
from
'../build/entries'
import
{
watchCompilers
}
from
'../build/output'
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
{
BLOCKED_PAGES
,
...
...
@@ -46,6 +46,12 @@ export async function renderScriptError(res: ServerResponse, error: Error) {
}
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
)
{
return
{
preflight
:
false
}
}
...
...
test/integration/api-support/pages/api/cors.js
0 → 100644
View file @
57dcdd90
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!'
})
}
test/integration/api-support/test/index.test.js
View file @
57dcdd90
...
...
@@ -44,6 +44,20 @@ function runTests(dev = false) {
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
()
=>
{
const
text
=
await
fetchViaHTTP
(
appPort
,
'/api'
,
null
,
{}).
then
(
res
=>
res
.
ok
&&
res
.
text
()
...
...
yarn.lock
View file @
57dcdd90
...
...
@@ -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"
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:
version "2.2.2"
resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-2.2.2.tgz#6173cebd56fac042c1f4390edf7af6c07c7cb892"
...
...
@@ -12568,7 +12576,7 @@ oauth-sign@~0.9.0:
resolved "https://registry.yarnpkg.com/oauth-sign/-/oauth-sign-0.9.0.tgz#47a7b016baa68b5fa0ecf3dee08a85c679ac6455"
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"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
integrity sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=
...
...
@@ -18064,7 +18072,7 @@ validate-npm-package-name@3.0.0, validate-npm-package-name@^3.0.0:
dependencies:
builtins "^1.0.3"
vary@~1.1.2:
vary@
^1, vary@
~1.1.2:
version "1.1.2"
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
integrity sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment