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
463d30ec
Unverified
Commit
463d30ec
authored
Dec 17, 2019
by
JJ Kasper
Committed by
GitHub
Dec 17, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add handling for external redirects (#9764)
parent
dbc0853e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
3 deletions
+34
-3
check-custom-routes.ts
packages/next/lib/check-custom-routes.ts
+1
-1
next-server.ts
packages/next/next-server/server/next-server.ts
+13
-2
next.config.js
test/integration/custom-routes/next.config.js
+4
-0
index.test.js
test/integration/custom-routes/test/index.test.js
+16
-0
No files found.
packages/next/lib/check-custom-routes.ts
View file @
463d30ec
...
@@ -40,7 +40,7 @@ export default function checkCustomRoutes(
...
@@ -40,7 +40,7 @@ export default function checkCustomRoutes(
invalidParts
.
push
(
'`destination` is missing'
)
invalidParts
.
push
(
'`destination` is missing'
)
}
else
if
(
typeof
route
.
destination
!==
'string'
)
{
}
else
if
(
typeof
route
.
destination
!==
'string'
)
{
invalidParts
.
push
(
'`destination` is not a string'
)
invalidParts
.
push
(
'`destination` is not a string'
)
}
else
if
(
!
route
.
destination
.
startsWith
(
'/'
))
{
}
else
if
(
type
===
'rewrite'
&&
!
route
.
destination
.
startsWith
(
'/'
))
{
invalidParts
.
push
(
'`destination` does not start with /'
)
invalidParts
.
push
(
'`destination` does not start with /'
)
}
}
...
...
packages/next/next-server/server/next-server.ts
View file @
463d30ec
...
@@ -404,7 +404,10 @@ export default class Server {
...
@@ -404,7 +404,10 @@ export default class Server {
statusCode
:
route
.
statusCode
,
statusCode
:
route
.
statusCode
,
name
:
`
${
route
.
type
}
${
route
.
source
}
route`
,
name
:
`
${
route
.
type
}
${
route
.
source
}
route`
,
fn
:
async
(
_req
,
res
,
params
,
_parsedUrl
)
=>
{
fn
:
async
(
_req
,
res
,
params
,
_parsedUrl
)
=>
{
let
destinationCompiler
=
compilePathToRegex
(
route
.
destination
)
const
parsedDestination
=
parseUrl
(
route
.
destination
,
true
)
let
destinationCompiler
=
compilePathToRegex
(
`
${
parsedDestination
.
pathname
!
}${
parsedDestination
.
hash
||
''
}
`
)
let
newUrl
let
newUrl
try
{
try
{
...
@@ -423,7 +426,15 @@ export default class Server {
...
@@ -423,7 +426,15 @@ export default class Server {
}
}
if
(
route
.
type
===
'redirect'
)
{
if
(
route
.
type
===
'redirect'
)
{
res
.
setHeader
(
'Location'
,
newUrl
)
const
parsedNewUrl
=
parseUrl
(
newUrl
)
res
.
setHeader
(
'Location'
,
formatUrl
({
...
parsedDestination
,
pathname
:
parsedNewUrl
.
pathname
,
hash
:
parsedNewUrl
.
hash
,
})
)
res
.
statusCode
=
route
.
statusCode
||
DEFAULT_REDIRECT_STATUS
res
.
statusCode
=
route
.
statusCode
||
DEFAULT_REDIRECT_STATUS
res
.
end
()
res
.
end
()
return
{
return
{
...
...
test/integration/custom-routes/next.config.js
View file @
463d30ec
...
@@ -92,6 +92,10 @@ module.exports = {
...
@@ -92,6 +92,10 @@ module.exports = {
destination
:
'/'
,
destination
:
'/'
,
statusCode
:
303
,
statusCode
:
303
,
},
},
{
source
:
'/to-external'
,
destination
:
'https://google.com'
,
},
]
]
},
},
},
},
...
...
test/integration/custom-routes/test/index.test.js
View file @
463d30ec
...
@@ -152,6 +152,15 @@ const runTests = (isDev = false) => {
...
@@ -152,6 +152,15 @@ const runTests = (isDev = false) => {
expect
(
await
getBrowserBodyText
(
browser
)).
toMatch
(
/Hello again/
)
expect
(
await
getBrowserBodyText
(
browser
)).
toMatch
(
/Hello again/
)
})
})
it
(
'should allow redirecting to external resource'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
appPort
,
'/to-external'
,
undefined
,
{
redirect
:
'manual'
,
})
const
location
=
res
.
headers
.
get
(
'location'
)
expect
(
res
.
status
).
toBe
(
307
)
expect
(
location
).
toBe
(
'https://google.com/'
)
})
if
(
!
isDev
)
{
if
(
!
isDev
)
{
it
(
'should output routes-manifest successfully'
,
async
()
=>
{
it
(
'should output routes-manifest successfully'
,
async
()
=>
{
const
manifest
=
await
fs
.
readJSON
(
const
manifest
=
await
fs
.
readJSON
(
...
@@ -237,6 +246,13 @@ const runTests = (isDev = false) => {
...
@@ -237,6 +246,13 @@ const runTests = (isDev = false) => {
regex
:
normalizeRegEx
(
'^
\\
/redir-chain3$'
),
regex
:
normalizeRegEx
(
'^
\\
/redir-chain3$'
),
regexKeys
:
[],
regexKeys
:
[],
},
},
{
destination
:
'https://google.com'
,
regex
:
normalizeRegEx
(
'^
\\
/to-external$'
),
regexKeys
:
[],
source
:
'/to-external'
,
statusCode
:
307
,
},
],
],
rewrites
:
[
rewrites
:
[
{
{
...
...
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