Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iron Router redirect without killing back button functionality

I am using Iron Router in a fairly complex app and have some routes that redirect users to other internal routes (for example, "/" always redirects to "/dashboard").

We have been dealing with this by just adding e.g. Router.go("/dashboard"), or (confusingly but equally ineffective) this.redirect("/dashboard") to the first route's action hook.

The problem is that this breaks the browser's back button: you fleetingly land back on the route that has the redirect action, which then redirects you right back to where you were.

  1. What is the best way around this?
  2. Is there any good reason why we shouldn't just replace rather than push the history.state by default for this.redirect()?
like image 649
ephemer Avatar asked Mar 19 '23 03:03

ephemer


2 Answers

We had the same problem in our app. In 0.9.4 (and probably some earlier versions), you can add {replaceState: true} as the last argument to Router.go. For more details, see this issue. Note that as of this writing, it looks like this has not been integrated into the 1.0.x code.

like image 140
David Weldon Avatar answered Apr 06 '23 01:04

David Weldon


For anyone still wondering, a fix for this issue is scheduled for the next release of Iron Router: https://github.com/EventedMind/iron-router/issues/764

There you will be able to use this.redirect() from within your route hooks, which will replace the state by default

like image 29
ephemer Avatar answered Apr 06 '23 00:04

ephemer