Skip to content

bug: canActivate does not cancel navigation when using browser back button #43181

Closed

Description

@liamdebeasi

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

router

Is this a regression?

No

Description

When using the browser back button to go back to a page with a canActivate guard, navigation is not correctly prevented if the guard returns false and the back button is pressed multiple times.

Steps to reproduce:

  1. Clone attached reproduction, run npm install, and then run npm run start.
  2. In the browser, you should start on /page1.
  3. Click "Go to Page 2". Observe you are on /page2.
  4. Click "Go to Page 3". Observe you are on /page3.
  5. Click "Disable going back to page 2". This will set a flag that causes the canActivate guard on /page2 to return false.
  6. Click the browser back button. Observe that the canActivate guard returns false and navigation is correctly cancelled. You should remain on /page3.
  7. Click the browser back button again. Observe that the canActivate guard is not called and navigation is not cancelled. You should now be on /page1.

Please provide a link to a minimal reproduction of the bug

https://github.com/liamdebeasi/ng-guard-repro

Please provide the exception or error you saw

I would expect that pressing the browser back button would attempt to navigate back to `/page2`, causing the `canActivate` guard to return `false` again.

Please provide the environment you discovered this bug in

Angular CLI: 12.2.1
Node: 15.9.0 (Unsupported)
Package Manager: npm 7.5.3
OS: darwin x64

Angular: 12.2.1
... cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1202.1
@angular-devkit/build-angular   12.2.1
@angular-devkit/core            12.2.1
@angular-devkit/schematics      12.2.1
@schematics/angular             12.2.1
rxjs                            6.6.7
typescript                      4.2.4

Anything else?

No response

Activity

  1. added this to the needsTriage milestone on Aug 16, 2021
  2. atscott commented on Aug 17, 2021

    @atscott
    Contributor

    Duplicate of #13586

  3. angular-automatic-lock-bot commented on Sep 17, 2021

    @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.

  4. locked and limited conversation to collaborators on Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions