[Solved] Dynamic import route with Blaze (with different layout)

Hello,
I am using blaze with dynamic imports, this is working fine when I don’t specify a layout
This is working fine (except the text iron_core.js Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?)

But now I would like to setup a layout and this goes into infinite loop :

Do you know how to handle this ?

This seems to work (setting the layout before the import callback):

Router.route('/test-route-with-layout-not-working', function () {
  this.layout('mobileLayout');
  import('./test-route.js').then(() => {
    this.render('testRoute');
  });
});```

Hi @dokithonon, so this worked for you?

1 Like

Yes this worked for me.

Probably the best way to do this with IronRouter is using their waitOn option.
iron-meteor.github.io/iron-router/#the-waiton-option

Unfortunately they don’t support the usual async handling, and instead use Meteor’s reactivity, re-running the function when a dependency changes and waiting until the function returns truthy.

This means you’ll need to use a ReactiveVar to get it working:

Router.route('/test-route-with-layout-not-working', {
  loadingTemplate: 'loading',
  waitOn() {
    // Return a function that gets run reactively
    return () => {
      // when we're loaded return true to continue. This also registers a dependency on state
      if (this.state.get('testRouteLoaded')) return true;

      import('./test-route.js').then(() => {
        // changing the state will get waitOn to re-run, which will return true
        this.state.set('testRouteLoaded', true);
      });
    };
  },
  action() {
    this.layout('mobileLayout');
    this.render('testRoute');
});
1 Like