Skip to content

APP_BASE_HREF doesn't work with routes which starts with the same text part #45744

Closed

Description

@Dolgovec

Which @angular/* package(s) are the source of the bug?

router

Is this a regression?

No

Description

If route starts with the same part as APP_BASE_HREF, the route becomes broken. E.g.:

app.module

{
      provide: APP_BASE_HREF,
      useValue: '/ru'
    }

app-routing

{
    path: 'rules',
    loadChildren: () => import('./pages/text-pages/rules/rules.module').then(m => m.RulesModule)
  },

And if you try to navigate example.com/rules you will get example.com/ru/les

Please, take a look at the stackblitz reproduction below.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/angular-ivy-68vpxc?file=src%2Fapp%2Fapp-routing.module.ts

Please provide the exception or error you saw

ERROR
Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'les'
Error: Cannot match any routes. URL Segment: 'les'

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

No response

Activity

  1. atscott commented on Apr 25, 2022

    @atscott
    Contributor

    This is being caused by the Location class, specifically the stripBaseHref logic:

    function _stripBaseHref(baseHref: string, url: string): string {
    return baseHref && url.startsWith(baseHref) ? url.substring(baseHref.length) : url;
    }

    Interestingly, APP_BASE_HREF is not used unless RouterModule or UpgradeModule is being used:

    export function provideLocationStrategy(platformLocation: PlatformLocation) {
    // See #23917
    const location = ɵɵinject(DOCUMENT).location;
    return new PathLocationStrategy(
    ɵɵinject(PlatformLocation as any), location && location.origin || '');
    }

    Even more minimal repo: https://stackblitz.com/edit/angular-ivy-6wsoys?file=src%2Fapp%2Fapp.module.ts
    Navigate to /rules and the path with display les rather than rules

  2. added
    area: common
    on Apr 25, 2022
  3. modified the milestone: needsTriage on Apr 25, 2022
  4. atscott commented on Apr 25, 2022

    @atscott
    Contributor

    As a workaround, you can provide your own Location (copy-paste the existing implementation and adjust the normalize/stripBaseHref)

  5. added
    P3
    on Apr 25, 2022
  6. modified the milestones: needsTriage, Backlog on Apr 25, 2022
  7. Dolgovec commented on Apr 25, 2022

    @Dolgovec
    Author

    @atscott Thx for your answer and explanation! Workaround works fine for me

  8. added a commit that references this issue on Dec 10, 2022
    ae0efb6
  9. added a commit that references this issue on Dec 12, 2022
    59c6bfb
  10. added a commit that references this issue on Jan 10, 2023
    5c3e4d6
  11. angular-automatic-lock-bot commented on Jan 12, 2023

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  12. locked and limited conversation to collaborators on Jan 12, 2023
  13. added 2 commits that reference this issue on Jan 12, 2023
    8802b4a
    68ce4f6
  14. added 2 commits that reference this issue on Feb 1, 2023
    5bdde06
    58b2290
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3area: commonarea: router

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions