Skip to content

Router should push to history if the state differs from the previous when the url is the same #36128

Closed
@ayeletdn

Description

@ayeletdn

🐞 bug report

Affected Package

The issue is caused by package @angular/router

Is this a regression?

I'm not sure

Description

When using a resolver to load a resource and the resource fails, trying to redirect the user to the same route they are currently on does not set the router's `state`.
this.router.navigate(['route1'], { state: { resolveFailed: path } });
                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Use case:

A table shows the user a list of items. The user requests to load a given item using the resolver, but the item no longer exists on the list. The API will return an error which requires preventing the user from navigating to the resource's page, but still showing an error message. Setting the state on the navigation extras can be used exactly for this purpose.

🔬 Minimal Reproduction

Reproduced issue on Stackblitz

Steps:

  1. click on route1 button, text should say Route 1 State: {"fromPage": "main", "navigationId": ...}
  2. click on route2 button, text should say Route 2 State: {"fromPage": "main", "navigationId": ...}
  3. click again on route1 button
  4. click on route3 button, text should say Route 2, State: { "resolveFailed": "https://api.example.com", "navigationId": ... }
  5. click on route1 button
  6. click on route2 button, text should say Route 2 State: {"fromPage": "main", "navigationId": ...}
  7. click on route 3 button:

Text should say Route 2 State: {"resolveFailed": "https://api.example.com", "navigationId": ...}
Actual text displayed: Route 2 State: {"fromPage": "main", "navigationId": ...}

Explanation:

Route 3 has a resolver which connects to a resource that doesn't exist, that in turn throws an error, caught by catchError rxjs operator. The catchError redirects the user back to route2.

  • If the user asked to navigate to the route from route1, then the state will be set successfully, as seen in step 4.
  • If the request was made from route2, then the user doesn't effectively change route and the state doesn't get set.

🌍 Your Environment

Angular Version:




8.2.14

Metadata

Metadata

Assignees

No one assigned

    Labels

    P5The team acknowledges the request but does not plan to address it, it remains open for discussionarea: routerfeatureIssue that requests a new featurefeature: insufficient votesLabel to add when the not a sufficient number of votes or comments from unique authorsfeature: votes requiredFeature request which is currently still in the voting phasefreq1: lowrouter: navigation pipeevents/scheduleNavigation/transitions observablestate: confirmed

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions