FlowRouter: how to make entry point configurable depending on environment?

I’m building a app that will run in the web browser as well as on mobile devices (Cordova) and I would like to set a different root (/) route depending on the actual environment.

I tried it this way:

  • Detect mobile environment in a client-side Meteor.startup() function (using Meteor.isCordova and navigator.platform detection) and set Session variables accordingly
  • Retrieve these Session variables in the FlowRouter.route(), which is located under /lib

This did work, but I’m not convinced with this solution since docs state that code under /lib is always called before any code under /client or /server. So it may be just a “lucky” chance that my Session variables were set before FlowRouter.route() is being called.

Hence, I tried to improve this by moving the actual detection code (and the setting of the Session variables based on detection results) into a startup.js under /lib. This, however, causes a rather “unpredictable” behaviour. Sometimes, the startup code (still encapsulated in Meteor.startup()) is run before the FlowRouter.route(), sometimes after it. I’m just guessing that this depends on when Meteor actually loads the code.

I could also try to set the Session variables on a global level (i.e. outside Meteor.startup()), but I’m unsure if this is a “safe” way, since I would guess that intializing global objects like “Session” is not “guaranteed” at that early time.

So, what would be the “safe” way to set environmental Session variables that can be retrieved by FlowRouter’s routes to react on the current environment?

1 Like

I am quite not sure how to do it. But, you can start FlowRouter after you’ve done some initialization.

See: https://github.com/kadirahq/flow-router#flowrouterwait-and-flowrouterinitialize

I hope this will help you.

@arunoda: Thanks a lot for your very fast reply. You really deserve your “professor” title :smile:

1 Like

haha :smile:
Not bad. I bet then everyone trying to comment often to get the Professor title :smile:

Just another question, if I may: According to the FlowRouter.wait() doc, I assume it is safe to call the FlowRouter.wait() outside a Meteor.startup() block? I’m just a bit unsure which global objects are expected to be available outside of Meteor.startup(), i.e. when exactly Meteor calls these “global lines of code” (i.e. if it is guaranteed that my own code is only run after Meteor has initialized itself and all of its packages, at least regarding set-up of all related JS classes). I could not find any info on that.

You need to call FlowRouter.wait() outside of Meteor.startup. Then it will call just after the JS file got executed. FlowRouter initialize inside a Meteor.startup block automatically.

Then, if you wait it, it won’t initialize it automatically.
Anyway, read the source: https://github.com/kadirahq/flow-router/blob/master/client/_init.js

Does this mean I can ignore the stipulation that a Cordova plugin must be called from within Meteor.startup (assuming the code I’m calling it from is routed)?