Skip to content

Commit 91684ee

Browse files
authored
Remove rewrite query params from request URL when deployed to Vercel (#76548)
1 parent dbeeb02 commit 91684ee

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

packages/next/src/server/server-utils.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ export function normalizeVercelUrl(
2727
paramKeys: string[],
2828
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
2929
) {
30-
if (!defaultRouteRegex) return
31-
32-
// make sure to normalize req.url on Vercel to strip dynamic params
33-
// from the query which are added during routing
30+
// make sure to normalize req.url on Vercel to strip dynamic and rewrite
31+
// params from the query which are added during routing
3432
const _parsedUrl = parseUrl(req.url!, true)
3533
delete (_parsedUrl as any).search
3634

@@ -45,7 +43,8 @@ export function normalizeVercelUrl(
4543
if (
4644
isNextQueryPrefix ||
4745
isNextInterceptionMarkerPrefix ||
48-
(paramKeys || Object.keys(defaultRouteRegex.groups)).includes(key)
46+
paramKeys.includes(key) ||
47+
(defaultRouteRegex && Object.keys(defaultRouteRegex.groups).includes(key))
4948
) {
5049
delete _parsedUrl.query[key]
5150
}

test/e2e/getserversideprops/app/next.config.js

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ module.exports = {
1414
source: '/blog-:param',
1515
destination: '/blog/post-3',
1616
},
17+
{
18+
source: '/rewrite-source/:path+',
19+
destination: '/rewrite-target',
20+
},
1721
]
1822
},
1923
}

test/e2e/getserversideprops/app/pages/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ const Page = ({ world, time, url }) => {
9595
</Link>
9696
<br />
9797
<Link href="/redirect-page">to redirect-page</Link>
98+
<br />
99+
<Link href="/rewrite-source/foo">to rewrite-source/foo</Link>
98100
</>
99101
)
100102
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useRouter } from 'next/router'
2+
3+
export async function getServerSideProps({ req }) {
4+
return { props: { url: req.url } }
5+
}
6+
7+
export default function RewriteTarget({ url }) {
8+
const router = useRouter()
9+
10+
return (
11+
<>
12+
<h1>rewrite-target</h1>
13+
<p id="as-path">{router.asPath}</p>
14+
<p id="req-url">{url}</p>
15+
</>
16+
)
17+
}

test/e2e/getserversideprops/test/index.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,12 @@ const expectedManifestRoutes = () => [
169169
),
170170
page: '/refresh',
171171
},
172+
{
173+
dataRouteRegex: normalizeRegEx(
174+
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/rewrite-target\\.json$`
175+
),
176+
page: '/rewrite-target',
177+
},
172178
{
173179
dataRouteRegex: normalizeRegEx(
174180
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/slow\\.json$`
@@ -543,6 +549,13 @@ const runTests = (isDev = false, isDeploy = false) => {
543549
expect($('#as-path').text()).toBe('/something')
544550
})
545551

552+
it('should not include rewrite query params in `asPath` and `req.url`', async () => {
553+
const $ = await next.render$('/rewrite-source/foo')
554+
expect($('h1').text()).toBe('rewrite-target')
555+
expect($('#as-path').text()).toBe('/rewrite-source/foo')
556+
expect($('#req-url').text()).toBe('/rewrite-source/foo')
557+
})
558+
546559
it('should return data correctly', async () => {
547560
const data = JSON.parse(
548561
await renderViaHTTP(next.url, `/_next/data/${buildId}/something.json`)

0 commit comments

Comments
 (0)