Iron Router's OnBeforeAction - how to find action that triggered it


#1

The title says it all. How can I find the action that triggered the call to OnBeforeAction within the OnBeforeAction function?

I would like to distinguish between the user going to the page or leaving the page (back button in browser).


#2

This is from memory so apologies if I’m not entirely correct:

Iron-router actions’s don’t directly triggered onBeforeAction’s to be run. If a new route is triggered via something like Router.go() or if a reactive data source used within an onBeforeAction changes then the routes onBeforeAction will be run/re-run.

Because of the second point it would not be uncommon for onBeforeAction to re-run even when the route doesn’t change. It is also fairly difficult to determine why the onBeforeAction has run, or at the very least I don’t think it is necessarily possible to determine if it was caused by a back button press.

With Iron-Router’s onStop and onRun methods you can determine when a route is being left or first being run respectively. These run only once per route so they are more resilient to code that is examining route state.

With regards to your back button query it seems that the using the browser onpopstate outside of iron-router might be a better idea. It won’t tell you whether the browser went back with a history.back() call or a back button press but it will tell you if you went back in history.

You could set a reactive variable with this and read that variable in iron-router.