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
5f480670
Unverified
Commit
5f480670
authored
Apr 09, 2025
by
Jiachi Liu
Committed by
GitHub
Apr 09, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[next-server] fix params duplicate in query after rewrite (#77939)
parent
e961a3b9
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
111 additions
and
5 deletions
+111
-5
base-server.ts
packages/next/src/server/base-server.ts
+19
-5
page.tsx
...ite-with-search-params/app/[domain]/[...section]/page.tsx
+25
-0
layout.tsx
test/e2e/app-dir/rewrite-with-search-params/app/layout.tsx
+11
-0
next.config.js
test/e2e/app-dir/rewrite-with-search-params/next.config.js
+21
-0
rewrite-with-search-params.test.ts
...ite-with-search-params/rewrite-with-search-params.test.ts
+35
-0
No files found.
packages/next/src/server/base-server.ts
View file @
5f480670
...
...
@@ -1196,21 +1196,26 @@ export default abstract class Server<
parsedUrl
.
pathname
=
`/
${
defaultLocale
}${
parsedUrl
.
pathname
}
`
}
// Store a copy of `parsedUrl.query` before calling handleRewrites.
// Since `handleRewrites` might add new queries to `parsedUrl.query`.
const
originQueryParams
=
{
...
parsedUrl
.
query
}
const
pathnameBeforeRewrite
=
parsedUrl
.
pathname
const
rewriteParamKeys
=
Object
.
keys
(
utils
.
handleRewrites
(
req
,
parsedUrl
)
)
const
didRewrite
=
pathnameBeforeRewrite
!==
parsedUrl
.
pathname
if
(
didRewrite
&&
parsedUrl
.
pathname
)
{
addRequestMeta
(
req
,
'rewroteURL'
,
parsedUrl
.
pathname
)
}
// Create a copy of the query params to avoid mutating the original
// object. This prevents any overlapping query params that have the
// same normalized key from causing issues.
const
queryParams
=
{
...
parsedUrl
.
query
}
const
didRewrite
=
pathnameBeforeRewrite
!==
parsedUrl
.
pathname
if
(
didRewrite
&&
parsedUrl
.
pathname
)
{
addRequestMeta
(
req
,
'rewroteURL'
,
parsedUrl
.
pathname
)
}
const
routeParamKeys
=
new
Set
<
string
>
()
for
(
const
[
key
,
value
]
of
Object
.
entries
(
parsedUrl
.
query
))
{
const
normalizedKey
=
normalizeNextQueryParam
(
key
)
if
(
!
normalizedKey
)
continue
...
...
@@ -1218,6 +1223,7 @@ export default abstract class Server<
// Remove the prefixed key from the query params because we want
// to consume it for the dynamic route matcher.
delete
parsedUrl
.
query
[
key
]
routeParamKeys
.
add
(
normalizedKey
)
if
(
typeof
value
===
'undefined'
)
continue
...
...
@@ -1371,6 +1377,14 @@ export default abstract class Server<
...
Object
.
keys
(
utils
.
defaultRouteRegex
?.
groups
||
{}),
])
}
// Remove the route `params` keys from `parsedUrl.query` if they are
// not in the original query params.
// If it's used in both route `params` and query `searchParams`, it should be kept.
for
(
const
key
of
routeParamKeys
)
{
if
(
!
(
key
in
originQueryParams
))
{
delete
parsedUrl
.
query
[
key
]
}
}
parsedUrl
.
pathname
=
matchedPath
url
.
pathname
=
parsedUrl
.
pathname
finished
=
await
this
.
normalizeAndAttachMetadata
(
req
,
res
,
parsedUrl
)
...
...
test/e2e/app-dir/rewrite-with-search-params/app/[domain]/[...section]/page.tsx
0 → 100644
View file @
5f480670
export
default
async
function
Page
({
params
,
searchParams
,
}:
{
params
:
Promise
<
{
domain
:
string
;
section
:
string
[]
}
>
searchParams
:
Promise
<
{
[
key
:
string
]:
string
|
string
[]
}
>
})
{
const
resolvedParams
=
await
params
const
resolvedSearchParams
=
await
searchParams
return
(
<>
<
div
>
<
h2
>
Params:
</
h2
>
<
pre
id=
"params-value"
>
{
JSON
.
stringify
(
resolvedParams
,
null
,
2
)
}
</
pre
>
</
div
>
<
div
>
<
h2
>
Search Params:
</
h2
>
<
pre
id=
"search-params-value"
>
{
JSON
.
stringify
(
resolvedSearchParams
,
null
,
2
)
}
</
pre
>
</
div
>
</>
)
}
test/e2e/app-dir/rewrite-with-search-params/app/layout.tsx
0 → 100644
View file @
5f480670
export
default
function
RootLayout
({
children
,
}:
Readonly
<
{
children
:
React
.
ReactNode
}
>
)
{
return
(
<
html
>
<
body
>
{
children
}
</
body
>
</
html
>
)
}
test/e2e/app-dir/rewrite-with-search-params/next.config.js
0 → 100644
View file @
5f480670
/**
* @type {import('next').NextConfig}
*/
const
nextConfig
=
{
async
rewrites
()
{
return
[
{
source
:
'/:path((?!sitemap.xml$).*)'
,
has
:
[
{
type
:
'host'
,
value
:
'(?<subdomain>[^.]+)
\\
.(?<domain>.*)'
,
},
],
destination
:
'/:subdomain/:path*'
,
},
]
},
}
module
.
exports
=
nextConfig
test/e2e/app-dir/rewrite-with-search-params/rewrite-with-search-params.test.ts
0 → 100644
View file @
5f480670
import
{
nextTestSetup
}
from
'e2e-utils'
describe
(
'rewrite-with-search-params'
,
()
=>
{
const
{
next
,
isNextDeploy
}
=
nextTestSetup
({
files
:
__dirname
,
})
it
(
'should not contain params in search params after rewrite'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/galleries/123'
,
{
param
:
'value'
,
},
{
headers
:
isNextDeploy
?
undefined
:
{
host
:
'vercel-test.vercel.app'
,
},
}
)
const
searchParams
=
JSON
.
parse
(
$
(
'#search-params-value'
).
text
())
const
params
=
JSON
.
parse
(
$
(
'#params-value'
).
text
())
expect
(
searchParams
).
toEqual
({
param
:
'value'
,
})
expect
(
params
).
toEqual
({
domain
:
expect
.
stringMatching
(
/
[\w
-
]
+/
),
section
:
[
'galleries'
,
'123'
],
})
})
})
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