veliovgroup / flow-router

🚦 Carefully extended flow-router for Meteor
https://packosphere.com/ostrio/flow-router-extra
BSD 3-Clause "New" or "Revised" License
202 stars 29 forks source link

Dynamics imports and redirects #13

Closed ch-lukas closed 7 years ago

ch-lukas commented 7 years ago

Starting using Meteor 1.5 and this router to be able to get most out of dynamic imports and everything is working well... almost. Any suggestions?

When I redirect (e.g. if user is already logged-in, redirect from / to dashboard) I get a 'no such template' error and then even with the error the page loads up correctly.

Some background :

Tried these 2 options, but don't work

Open.route('/', {
    name: 'landingPage',
    triggersEnter: [function () {
        if (Meteor.userId()) {
                        // return import(' ....  eg dashboardPage');
            FlowRouter.go('/dashboard');
        }
    }],
    waitOn() {
        return import('/imports/ui/pages/landing');
    },
    action() {
        this.render('landingLayout', 'landingPage');
    }
});

Alternative,

Open.route('/', {
    name: 'landingPage',
    waitOn() {
        if (Meteor.userId()) {
            return import(' ....  eg dashboardPage');
        } else {
            return import('/imports/ui/pages/landing');
        }
    },
    action() {
        if (Meteor.userId()) {
            FlowRouter.go('/dashboard');
        } else {
            this.render('landingLayout', 'landingPage');
        }
    }
});
dr-dimitru commented 7 years ago

Hello @chluke123 ,

What is import(' .... eg dashboardPage'); exactly? Is this something what generated dynamically? Note: import, import() and require() can accept only string as argument, not a variable - only string. See #8717 , forum post, SO thread

ch-lukas commented 7 years ago

Hi @dr-dimitru,

That was just a placeholder value for illustration. A real life example is return import('/imports/ui/pages/dashboard/navigation');

In the navigation folder is an index that loads the templates etc.

Routes all works well, except in the instance of the initial root redirect.

At first glance, it seems as if the Flowrouter.go redirect is bypassing the hooks of the route you want to go to.

Cheers

dr-dimitru commented 7 years ago

Okay, then does it work this way?:

Open.route('/', {
    name: 'landingPage',
    waitOn() {
        return import('/imports/ui/pages/landing');
    },
    action() {
        if (Meteor.userId()) {
            FlowRouter.go('/dashboard');
        } else {
            this.render('landingLayout', 'landingPage');
        }
    }
});

Open.route('/dashboard', {
    name: 'dashboard Page',
    waitOn() {
        return import(' ....  eg dashboardPage');
    },
    action() {
        this.render('dashboardLayout', 'dashboardPage');
    }
});
ch-lukas commented 7 years ago

It works - cheers!

And so does my alternate example above... but I have been working on the code since then :).

The one constant is to watch out for the combination of triggersEnter / waitOn route imports / redirects.

Maybe a quick note in the docs about the limitations of using the triggersEnter hook might help.

Cheers, L

dr-dimitru commented 7 years ago

Well triggersEnter definitely isn't equal to waitOn. I'm glad it's solved.

Feel free to reopen it in case if the issue still persists on your end.

Please, support this project by: