question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

🐞 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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
ayeletdncommented, Mar 20, 2020

Agree. Done.

0reactions
angular-automatic-lock-bot[bot]commented, Sep 4, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react-router Redirect vs history.push - Stack Overflow
Rendering a <Redirect> will navigate to a new location. The new location will override the current location in the history stack, like server-Β ......
Read more >
Working with history, pushState and replaceState in Javascript
The big difference is, that while pushState will create a new entry in the browser's history, replaceState will onle replace the current state.Β ......
Read more >
History - React Router: Declarative Routing for React.js
push (path, state) when this location was pushed onto the stack. Only available in browser and memory history.
Read more >
History.pushState() - Web APIs | MDN
pushState() method adds an entry to the browser's session history stack. ... The new URL must be of the same origin as the...
Read more >
Modern client-side routing: the Navigation API
If you've ever written the routing for your own SPA using the History API, ... is same-document, and the hash is the only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found