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
ef379f15
Unverified
Commit
ef379f15
authored
Jun 17, 2021
by
JJ Kasper
Committed by
GitHub
Jun 17, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure has query encoding is normalized (#25732)
* Ensure has query encoding is normalized * fix type error
parent
b3fad232
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
83 additions
and
6 deletions
+83
-6
prepare-destination.ts
.../next/next-server/lib/router/utils/prepare-destination.ts
+16
-0
next.config.js
test/integration/custom-routes/next.config.js
+10
-0
index.js
test/integration/custom-routes/pages/blog/[post]/index.js
+9
-5
index.test.js
test/integration/custom-routes/test/index.test.js
+47
-0
next-test-utils.js
test/lib/next-test-utils.js
+1
-1
No files found.
packages/next/next-server/lib/router/utils/prepare-destination.ts
View file @
ef379f15
...
...
@@ -31,6 +31,17 @@ export function matchHas(
query
:
Params
):
false
|
Params
{
const
params
:
Params
=
{}
let
initialQueryValues
:
string
[]
=
[]
if
(
typeof
window
===
'undefined'
)
{
initialQueryValues
=
Object
.
values
((
req
as
any
).
__NEXT_INIT_QUERY
)
}
if
(
typeof
window
!==
'undefined'
)
{
initialQueryValues
=
Array
.
from
(
new
URLSearchParams
(
location
.
search
).
values
()
)
}
const
allMatch
=
has
.
every
((
hasItem
)
=>
{
let
value
:
undefined
|
string
let
key
=
hasItem
.
key
...
...
@@ -46,7 +57,12 @@ export function matchHas(
break
}
case
'query'
:
{
// preserve initial encoding of query values
value
=
query
[
key
!
]
if
(
initialQueryValues
.
includes
(
value
||
''
))
{
value
=
encodeURIComponent
(
value
!
)
}
break
}
case
'host'
:
{
...
...
test/integration/custom-routes/next.config.js
View file @
ef379f15
...
...
@@ -190,6 +190,16 @@ module.exports = {
],
destination
:
'/with-params?idk=:idk'
,
},
{
source
:
'/has-rewrite-8'
,
has
:
[
{
type
:
'query'
,
key
:
'post'
,
},
],
destination
:
'/blog/:post'
,
},
{
source
:
'/blog/about'
,
destination
:
'/hello'
,
...
...
test/integration/custom-routes/pages/blog/[post]/index.js
View file @
ef379f15
import
{
useRouter
}
from
'next/router'
const
Page
=
()
=>
(
<>
<
p
>
post
:
{
useRouter
().
query
.
post
}
<
/p
>
<
/
>
)
const
Page
=
()
=>
{
const
{
query
}
=
useRouter
()
return
(
<>
<
p
>
post
:
{
query
.
post
}
<
/p
>
<
div
id
=
"query"
>
{
JSON
.
stringify
(
query
)}
<
/div
>
<
/
>
)
}
Page
.
getInitialProps
=
()
=>
({
hello
:
'world'
})
...
...
test/integration/custom-routes/test/index.test.js
View file @
ef379f15
...
...
@@ -39,6 +39,42 @@ let appPort
let
app
const
runTests
=
(
isDev
=
false
)
=>
{
it
(
'should handle has query encoding correctly'
,
async
()
=>
{
for
(
const
expected
of
[
{
post
:
'first'
,
},
{
post
:
'hello%20world'
,
},
{
post
:
'hello/world'
,
},
{
post
:
'hello%2fworld'
,
},
])
{
const
{
status
=
200
,
post
}
=
expected
const
res
=
await
fetchViaHTTP
(
appPort
,
'/has-rewrite-8'
,
`?post=
${
post
}
`
,
{
redirect
:
'manual'
,
}
)
expect
(
res
.
status
).
toBe
(
status
)
if
(
status
===
200
)
{
const
$
=
cheerio
.
load
(
await
res
.
text
())
expect
(
JSON
.
parse
(
$
(
'#query'
).
text
())).
toEqual
({
post
:
decodeURIComponent
(
post
),
})
}
}
})
it
(
'should support long URLs for rewrites'
,
async
()
=>
{
const
res
=
await
fetchViaHTTP
(
appPort
,
...
...
@@ -1718,6 +1754,17 @@ const runTests = (isDev = false) => {
regex
:
normalizeRegEx
(
'^
\\
/has-rewrite-7$'
),
source
:
'/has-rewrite-7'
,
},
{
destination
:
'/blog/:post'
,
has
:
[
{
key
:
'post'
,
type
:
'query'
,
},
],
regex
:
normalizeRegEx
(
'^
\\
/has-rewrite-8$'
),
source
:
'/has-rewrite-8'
,
},
{
destination
:
'/hello'
,
regex
:
normalizeRegEx
(
'^
\\
/blog
\\
/about$'
),
...
...
test/lib/next-test-utils.js
View file @
ef379f15
...
...
@@ -83,7 +83,7 @@ export function renderViaHTTP(appPort, pathname, query, opts) {
export
function
fetchViaHTTP
(
appPort
,
pathname
,
query
,
opts
)
{
const
url
=
`http://localhost:
${
appPort
}${
pathname
}${
query
?
`?
${
qs
.
stringify
(
query
)}
`
:
''
typeof
query
===
'string'
?
query
:
query
?
`?
${
qs
.
stringify
(
query
)}
`
:
''
}
`
return
fetch
(
url
,
opts
)
}
...
...
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