Skip to content

Wildcard '**' routes should run canMatch guard #49949

@alienwareone

Description

@alienwareone

Which @angular/* package(s) are relevant/related to the feature request?

router

Description

I would expect that wildcard '**' routes would run the canMatch guard.

const routes: Routes = [
  { path: 'path', component: Page1, canMatch: [() => true] },
  {
    path: '**',
    component: Page2,
    canMatch: [(route: Route, segments: UrlSegment[]) => segments.toString() === 'page2']
  },
  {
    path: '**',
    component: Page3,
    canMatch: [(route: Route, segments: UrlSegment[]) => segments.toString() === 'page3']
  },
];

Stackblitz sample: https://stackblitz.com/edit/angular-azxep5?file=src%2Fmain.ts

Proposed solution

Wildcard '**' should run the canMatch guard so we can register multiple wildcard routes (same behavior like the workaround bellow).
(Personally I use this for multiple slug based features without using a prefix path segment.)

Alternatives considered

To workaround this issue we can provide a matcher that always returns.

const routes: Routes = [
  { path: '', component: Page1, canMatch: [() => true] },
  {
    matcher: (segments: UrlSegment[]) => ({ consumed: segments }),
    component: Page2,
    canMatch: [
      (route: Route, segments: UrlSegment[]) => segments.toString() === 'page2',
    ],
  },
  {
    matcher: (segments: UrlSegment[]) => ({ consumed: segments }),
    component: Page3,
    canMatch: [
      (route: Route, segments: UrlSegment[]) => segments.toString() === 'page3',
    ],
  },
];

Stackblitz sample: https://stackblitz.com/edit/angular-bg6fca?file=src%2Fmain.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4A relatively minor issue that is not relevant to core functionsarea: router

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions