Unverified Commit 101a70ba authored by cvolant's avatar cvolant Committed by GitHub

Fix as option with optional catch all routes url object syntax (#42355)

In `Link`, `router.push`, and `router.replace`, `as` option used to
throw for optional catch all routes with url object syntax: optional
attribute were not taken into account when checking missing parameters
in router change method.
This PR just add a condition in the filter callback to filter out
optional routes.
Fixes #41624

## Bug

- [X] Related issues linked using `fixes #number`
- [X] Integration tests added
- [X] Errors have a helpful link attached, see `contributing.md`
parent 2b85da77
...@@ -1505,7 +1505,7 @@ export default class Router implements BaseRouter { ...@@ -1505,7 +1505,7 @@ export default class Router implements BaseRouter {
if (!routeMatch || (shouldInterpolate && !interpolatedAs.result)) { if (!routeMatch || (shouldInterpolate && !interpolatedAs.result)) {
const missingParams = Object.keys(routeRegex.groups).filter( const missingParams = Object.keys(routeRegex.groups).filter(
(param) => !query[param] (param) => !query[param] && !routeRegex.groups[param].optional
) )
if (missingParams.length > 0 && !isMiddlewareMatch) { if (missingParams.length > 0 && !isMiddlewareMatch) {
......
...@@ -5,6 +5,10 @@ module.exports = { ...@@ -5,6 +5,10 @@ module.exports = {
source: '/rewrite-me', source: '/rewrite-me',
destination: '/another', destination: '/another',
}, },
{
source: '/blog/:slugs*',
destination: '/news/:slugs*',
},
] ]
}, },
} }
import Link from 'next/link'
import { useRouter } from 'next/router'
export default function News() {
const { asPath, pathname, query } = useRouter()
return (
<div>
<h1 id="news">news page</h1>
<p id="asPath">{asPath}</p>
<p id="pathname">{pathname}</p>
<p id="query">{JSON.stringify(query)}</p>
<Link href="/">&larr; Back home</Link>
</div>
)
}
export async function getServerSideProps() {
return {
props: {},
}
}
...@@ -55,6 +55,15 @@ export default function Page(props) { ...@@ -55,6 +55,15 @@ export default function Page(props) {
go to /preview/321 as /rewrite-me go to /preview/321 as /rewrite-me
</Link> </Link>
<br /> <br />
<Link
href={{ pathname: '/news/[[...slugs]]', query: { slugs: [] } }}
as="/blog"
id="to-news-as-blog"
>
go to /news as /blog
</Link>
<br />
</> </>
) )
} }
...@@ -144,6 +144,18 @@ const runTests = () => { ...@@ -144,6 +144,18 @@ const runTests = () => {
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({ expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
slug: '321', slug: '321',
}) })
await browser.back().waitForElementByCss('#preview')
await browser
.elementByCss('#to-news-as-blog')
.click()
.waitForElementByCss('#news')
expect(await browser.elementByCss('#news').text()).toBe('news page')
expect(await browser.elementByCss('#asPath').text()).toBe('/blog')
expect(await browser.eval('window.beforeNav')).toBe(1)
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({})
}) })
} }
......
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