Skip to content

location.back() does not work on cancelled route #14645

Closed

Description

@michelherv

**I'm submitting a ...

[x] bug report
[ ] feature request
[ ] support request 

Current behavior
I'm using a guard to protect a route (View Two). User can go from View One to View Two. Guard checks if application has some state. If not, the guard redirect the user on a dedicated page (View State). On that page user will give the expected state to application. After that, I do a location.back(). This calls send back the user on page View One not on View Two.

Expected behavior
I will like that 'location.back()' back to the cancelled route (View Two)

Minimal reproduction of the problem with instructions
A demonstration on plunkr

What is the motivation / use case for changing the behavior?
In my application, there are optional pages (optional functionalities). Those pages can be accessed only if some configurations has been done before. When users try to navigate to those pages for the first time, I redirect them to the configuration pages. On those configuration pages, I want to use 'location.back()' to back to the cancelled routes.

Please tell us about your environment:

  • Environment: Windows 7, Atom, npm, Webpack-dev-server

  • Angular version: 2.4.8

  • Browser: all

  • Language: TypeScript 2.1.5

  • Node (for AoT issues): node --version = 6.9.5

Activity

  1. SamVerschueren commented on Mar 17, 2017

    @SamVerschueren
    Contributor

    I took a deep dive into the router in order to track down what causes this and I found some things. Just not sure how to fix this but thought I could share my findings already.

    So, the guards are executed right here. So when navigating to views/two, shouldActivate is false.

    So then the router goes to the next check, which is the preActivationResolveData step. But at that point, this check kicks in. The navigationId is incremented already. The reason for this is quite simple, at that point, your guard initiated a new navigation which in turns triggers scheduleNavigation which on his turn increments the navigation id.

    this.router.navigate(['/views/state']);

    So what I tried to do, just to check if it worked, was calling location.pushState() right here. It calls pushState correctly but location.back() still redirects to views/one.

  2. added this to the Backlog milestone on Jan 23, 2018
  3. atscott commented on May 26, 2020

    @atscott
    Contributor

    Duplicate of #13586

  4. angular-automatic-lock-bot commented on Jun 26, 2020

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

  5. locked and limited conversation to collaborators on Jun 26, 2020
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