mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
Remove rewrite query params from request URL when deployed to Vercel (#76548)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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]
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ module.exports = {
|
||||
source: '/blog-:param',
|
||||
destination: '/blog/post-3',
|
||||
},
|
||||
{
|
||||
source: '/rewrite-source/:path+',
|
||||
destination: '/rewrite-target',
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -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$`
|
||||
@@ -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`)
|
||||
|
||||
Reference in New Issue
Block a user