Question: How to remove previous route url
from history so that browser "back" button skip it?
In my application if data is invalid or other issues - Angular
performs redirection to error page. But if to press "Back" button in browser - user gets redirected to error page again and from there to error page. So it is kind of endless cycle.
The idea is to remove previous url
(with invalid data) from history so previous redirect would lead to the page before url
with invalid data.
We have some browser.location
api in vanilla JS
, but how to do that by Angular
way?
Update: there are many ways to get previous url, like: How to determine previous page URL in Angular?. But how to set it?
Solution: I've tried to use Angular
Location.replaceState()
to override
history. In some reason it just removed one history entry without replacing it: https://angular.io/api/common/Location#replaceState
Similar behavior was in vanilla JS
History.replaceState()
:
https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState
I end up with changing design to use skipLocationChange: true
in NavigationExtras
:
https://angular.io/api/router/NavigationExtras
so Bálint Réthy's answer fits best.