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
91684eea
Unverified
Commit
91684eea
authored
Feb 26, 2025
by
Hendrik Liebau
Committed by
GitHub
Feb 26, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Remove rewrite query params from request URL when deployed to Vercel (#76548)
parent
dbeeb02d
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
40 additions
and
5 deletions
+40
-5
server-utils.ts
packages/next/src/server/server-utils.ts
+4
-5
next.config.js
test/e2e/getserversideprops/app/next.config.js
+4
-0
index.js
test/e2e/getserversideprops/app/pages/index.js
+2
-0
index.js
.../e2e/getserversideprops/app/pages/rewrite-target/index.js
+17
-0
index.test.ts
test/e2e/getserversideprops/test/index.test.ts
+13
-0
No files found.
packages/next/src/server/server-utils.ts
View file @
91684eea
...
@@ -27,10 +27,8 @@ export function normalizeVercelUrl(
...
@@ -27,10 +27,8 @@ export function normalizeVercelUrl(
paramKeys
:
string
[],
paramKeys
:
string
[],
defaultRouteRegex
:
ReturnType
<
typeof
getNamedRouteRegex
>
|
undefined
defaultRouteRegex
:
ReturnType
<
typeof
getNamedRouteRegex
>
|
undefined
)
{
)
{
if
(
!
defaultRouteRegex
)
return
// make sure to normalize req.url on Vercel to strip dynamic and rewrite
// params from the query which are added during routing
// make sure to normalize req.url on Vercel to strip dynamic params
// from the query which are added during routing
const
_parsedUrl
=
parseUrl
(
req
.
url
!
,
true
)
const
_parsedUrl
=
parseUrl
(
req
.
url
!
,
true
)
delete
(
_parsedUrl
as
any
).
search
delete
(
_parsedUrl
as
any
).
search
...
@@ -45,7 +43,8 @@ export function normalizeVercelUrl(
...
@@ -45,7 +43,8 @@ export function normalizeVercelUrl(
if
(
if
(
isNextQueryPrefix
||
isNextQueryPrefix
||
isNextInterceptionMarkerPrefix
||
isNextInterceptionMarkerPrefix
||
(
paramKeys
||
Object
.
keys
(
defaultRouteRegex
.
groups
)).
includes
(
key
)
paramKeys
.
includes
(
key
)
||
(
defaultRouteRegex
&&
Object
.
keys
(
defaultRouteRegex
.
groups
).
includes
(
key
))
)
{
)
{
delete
_parsedUrl
.
query
[
key
]
delete
_parsedUrl
.
query
[
key
]
}
}
...
...
test/e2e/getserversideprops/app/next.config.js
View file @
91684eea
...
@@ -14,6 +14,10 @@ module.exports = {
...
@@ -14,6 +14,10 @@ module.exports = {
source
:
'/blog-:param'
,
source
:
'/blog-:param'
,
destination
:
'/blog/post-3'
,
destination
:
'/blog/post-3'
,
},
},
{
source
:
'/rewrite-source/:path+'
,
destination
:
'/rewrite-target'
,
},
]
]
},
},
}
}
test/e2e/getserversideprops/app/pages/index.js
View file @
91684eea
...
@@ -95,6 +95,8 @@ const Page = ({ world, time, url }) => {
...
@@ -95,6 +95,8 @@ const Page = ({ world, time, url }) => {
<
/Link
>
<
/Link
>
<
br
/>
<
br
/>
<
Link
href
=
"/redirect-page"
>
to
redirect
-
page
<
/Link
>
<
Link
href
=
"/redirect-page"
>
to
redirect
-
page
<
/Link
>
<
br
/>
<
Link
href
=
"/rewrite-source/foo"
>
to
rewrite
-
source
/
foo
<
/Link
>
<
/
>
<
/
>
)
)
}
}
...
...
test/e2e/getserversideprops/app/pages/rewrite-target/index.js
0 → 100644
View file @
91684eea
import
{
useRouter
}
from
'next/router'
export
async
function
getServerSideProps
({
req
})
{
return
{
props
:
{
url
:
req
.
url
}
}
}
export
default
function
RewriteTarget
({
url
})
{
const
router
=
useRouter
()
return
(
<>
<
h1
>
rewrite
-
target
<
/h1
>
<
p
id
=
"as-path"
>
{
router
.
asPath
}
<
/p
>
<
p
id
=
"req-url"
>
{
url
}
<
/p
>
<
/
>
)
}
test/e2e/getserversideprops/test/index.test.ts
View file @
91684eea
...
@@ -169,6 +169,12 @@ const expectedManifestRoutes = () => [
...
@@ -169,6 +169,12 @@ const expectedManifestRoutes = () => [
),
),
page
:
'/refresh'
,
page
:
'/refresh'
,
},
},
{
dataRouteRegex
:
normalizeRegEx
(
`^\\/_next\\/data\\/
${
escapeRegex
(
buildId
)}
\\/rewrite-target\\.json$`
),
page
:
'/rewrite-target'
,
},
{
{
dataRouteRegex
:
normalizeRegEx
(
dataRouteRegex
:
normalizeRegEx
(
`^\\/_next\\/data\\/
${
escapeRegex
(
buildId
)}
\\/slow\\.json$`
`^\\/_next\\/data\\/
${
escapeRegex
(
buildId
)}
\\/slow\\.json$`
...
@@ -543,6 +549,13 @@ const runTests = (isDev = false, isDeploy = false) => {
...
@@ -543,6 +549,13 @@ const runTests = (isDev = false, isDeploy = false) => {
expect
(
$
(
'#as-path'
).
text
()).
toBe
(
'/something'
)
expect
(
$
(
'#as-path'
).
text
()).
toBe
(
'/something'
)
})
})
it
(
'should not include rewrite query params in `asPath` and `req.url`'
,
async
()
=>
{
const
$
=
await
next
.
render$
(
'/rewrite-source/foo'
)
expect
(
$
(
'h1'
).
text
()).
toBe
(
'rewrite-target'
)
expect
(
$
(
'#as-path'
).
text
()).
toBe
(
'/rewrite-source/foo'
)
expect
(
$
(
'#req-url'
).
text
()).
toBe
(
'/rewrite-source/foo'
)
})
it
(
'should return data correctly'
,
async
()
=>
{
it
(
'should return data correctly'
,
async
()
=>
{
const
data
=
JSON
.
parse
(
const
data
=
JSON
.
parse
(
await
renderViaHTTP
(
next
.
url
,
`/_next/data/
${
buildId
}
/something.json`
)
await
renderViaHTTP
(
next
.
url
,
`/_next/data/
${
buildId
}
/something.json`
)
...
...
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