Skip to content

Commit

Permalink
Remove rewrite query params from request URL when deployed to Vercel (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
unstubbable authored Feb 26, 2025
1 parent dbeeb02 commit 91684ee
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/next/src/server/server-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,8 @@ export function normalizeVercelUrl(
paramKeys: string[],
defaultRouteRegex: ReturnType<typeof getNamedRouteRegex> | undefined
) {
if (!defaultRouteRegex) return

// make sure to normalize req.url on Vercel to strip dynamic params
// from the query which are added during routing
// make sure to normalize req.url on Vercel to strip dynamic and rewrite
// params from the query which are added during routing
const _parsedUrl = parseUrl(req.url!, true)
delete (_parsedUrl as any).search

Expand All @@ -45,7 +43,8 @@ export function normalizeVercelUrl(
if (
isNextQueryPrefix ||
isNextInterceptionMarkerPrefix ||
(paramKeys || Object.keys(defaultRouteRegex.groups)).includes(key)
paramKeys.includes(key) ||
(defaultRouteRegex && Object.keys(defaultRouteRegex.groups).includes(key))
) {
delete _parsedUrl.query[key]
}
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/getserversideprops/app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ module.exports = {
source: '/blog-:param',
destination: '/blog/post-3',
},
{
source: '/rewrite-source/:path+',
destination: '/rewrite-target',
},
]
},
}
2 changes: 2 additions & 0 deletions test/e2e/getserversideprops/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ const Page = ({ world, time, url }) => {
</Link>
<br />
<Link href="/redirect-page">to redirect-page</Link>
<br />
<Link href="/rewrite-source/foo">to rewrite-source/foo</Link>
</>
)
}
Expand Down
17 changes: 17 additions & 0 deletions test/e2e/getserversideprops/app/pages/rewrite-target/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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>
</>
)
}
13 changes: 13 additions & 0 deletions test/e2e/getserversideprops/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ const expectedManifestRoutes = () => [
),
page: '/refresh',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/rewrite-target\\.json$`
),
page: '/rewrite-target',
},
{
dataRouteRegex: normalizeRegEx(
`^\\/_next\\/data\\/${escapeRegex(buildId)}\\/slow\\.json$`
Expand Down Expand Up @@ -543,6 +549,13 @@ const runTests = (isDev = false, isDeploy = false) => {
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 () => {
const data = JSON.parse(
await renderViaHTTP(next.url, `/_next/data/${buildId}/something.json`)
Expand Down

0 comments on commit 91684ee

Please sign in to comment.