diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index e7d9610913e7..7e91cf88bf50 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -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) { diff --git a/test/integration/rewrites-manual-href-as/next.config.js b/test/integration/rewrites-manual-href-as/next.config.js index 0716516e76e6..da626b657914 100644 --- a/test/integration/rewrites-manual-href-as/next.config.js +++ b/test/integration/rewrites-manual-href-as/next.config.js @@ -5,6 +5,10 @@ module.exports = { source: '/rewrite-me', destination: '/another', }, + { + source: '/blog/:slugs*', + destination: '/news/:slugs*', + }, ] }, } diff --git a/test/integration/rewrites-manual-href-as/pages/news/[[...slugs]].js b/test/integration/rewrites-manual-href-as/pages/news/[[...slugs]].js new file mode 100644 index 000000000000..1d0a43584e4a --- /dev/null +++ b/test/integration/rewrites-manual-href-as/pages/news/[[...slugs]].js @@ -0,0 +1,22 @@ +import Link from 'next/link' +import { useRouter } from 'next/router' + +export default function News() { + const { asPath, pathname, query } = useRouter() + + return ( +
{asPath}
+{pathname}
+{JSON.stringify(query)}
+ ← Back home +