angular / angular

Deliver web apps with confidence 🚀
https://angular.dev
MIT License
95.34k stars 25.05k forks source link

Feature: redirect to/render a route without modifying browser history #8051

Closed simonbear89 closed 8 years ago

simonbear89 commented 8 years ago

Sorry if this issue has been raised before, I've looked everywhere I could!

Is there a way to redirect to/render a different route without modifying the browser history?

Say I have a route like this /store/products/:id which can have child routes like /store/products/:id/reviews. I follow a link to /store/products/34/reviews, I check with my server and the product no longer exists so I want to render a 404 page from my /404 route.

However, when I use router.navigate(['/404']) or router.navigateByUrl('/404') I end up at the 404 page, but both urls are added to my history.

So when I press the Back button in the browser, I go back to /store/products/34/review and I'm immediately redirected to /404, meaning I'm essentially stuck at the 404 page.

In addition to router.navigate() I have tried:

In ExpressJS, we can handle this by calling next() if the route param is invalid, so another handler can take care of it. Is there a client-side equivalent in Angular?

A similar example from #4112 http://plnkr.co/edit/tUhNqM2cNDp20ch7cezH?p=preview

If you open the example in a new window, make sure you're logged out, and navigate to .../#/parent using the browser address bar, you get redirected to the login component.

The Back button doesn't work anymore since it brings you back to .../#/parent which is immediately redirect back to the login page.

brandonroberts commented 8 years ago

@simonxca the second argument in router.navigateByUrl and router.navigateByInstruction is whether to skip the URL update.

    router.navigateByUrl('/404', true);

    router.navigateByInstruction(router.generate(['/404']), true);
simonbear89 commented 8 years ago

It works! That was easy thanks!

angular-automatic-lock-bot[bot] commented 4 years ago

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.