Unverified Commit 91684eea authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Remove rewrite query params from request URL when deployed to Vercel (#76548)

parent dbeeb02d
...@@ -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]
} }
......
...@@ -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',
},
] ]
}, },
} }
...@@ -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>
</> </>
) )
} }
......
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 = () => [ ...@@ -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`)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment