Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/shared/lib/router/router.ts
Expand Up @@ -1505,7 +1505,7 @@ export default class Router implements BaseRouter {

if (!routeMatch || (shouldInterpolate && !interpolatedAs.result)) {
const missingParams = Object.keys(routeRegex.groups).filter(
(param) => !query[param]
(param) => !query[param] && !routeRegex.groups[param].optional
)

if (missingParams.length > 0 && !isMiddlewareMatch) {
Expand Down
4 changes: 4 additions & 0 deletions test/integration/rewrites-manual-href-as/next.config.js
Expand Up @@ -5,6 +5,10 @@ module.exports = {
source: '/rewrite-me',
destination: '/another',
},
{
source: '/blog/:slugs*',
destination: '/news/:slugs*',
},
]
},
}
@@ -0,0 +1,22 @@
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: {},
}
}
Expand Up @@ -55,6 +55,15 @@ export default function Page(props) {
go to /preview/321 as /rewrite-me
</Link>
<br />

<Link
href={{ pathname: '/news/[[...slugs]]', query: { slugs: [] } }}
as="/blog"
id="to-news-as-blog"
>
go to /news as /blog
</Link>
<br />
</>
)
}
12 changes: 12 additions & 0 deletions test/integration/rewrites-manual-href-as/test/index.test.js
Expand Up @@ -144,6 +144,18 @@ const runTests = () => {
expect(JSON.parse(await browser.elementByCss('#query').text())).toEqual({
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({})
})
}

Expand Down