Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks called twice on every rendering ? Error or "Feature" ? #1031

Closed
TimoRuetten opened this issue Nov 9, 2014 · 15 comments
Closed

Hooks called twice on every rendering ? Error or "Feature" ? #1031

TimoRuetten opened this issue Nov 9, 2014 · 15 comments

Comments

@TimoRuetten
Copy link

Hey!

I switched from an older version to version 1.0 and now I have the problem that the Hooks called twice. Here is an example of what I mean:

This is my code for one route:

Router.route('/search/:page?/:what?/:where?', function () {
    var params;
    params = this.params;

    this.wait(Meteor.subscribe('business_search', {what : params.what, where : params.where}));

    if (this.ready()) {
        console.log('############### THIS.READY() ###############');
        this.render('search_result');
    } else {
        this.render('loaderDefault');
    }

    this.render('searchBig', {
        to : 'top',
        data : {
            what : params.what,
            where : params.where
        }
    });

}, {
    name : 'search',
    onBeforeAction : function () {
        console.log('onBeforeAction called');
        this.next();
    },
    onAfterAction : function () {
        console.log('onAFTERAction called');
    }
});

After running this route directly OR when clickung on a link which is "linking" to this route the console has this output:

onBeforeAction called
onAFTERAction called
onBeforeAction called
############### THIS.READY() ###############
onAFTERAction called

As you can see the onBeforeAction and also the OnAfterAction is always called twice.

I made a "test": When removing the "if (this.ready()) [...]" Block the Hooks are just called once per running ... so the this.ready() seems to be the problem ... but then I do not rly understand how to use this in the right way ?

@mizzao
Copy link
Contributor

mizzao commented Nov 12, 2014

I've also noticed this with waitOn getting called twice even when it triggers no reactive dependencies. Not sure if this is intended either.

@TimoRuetten
Copy link
Author

Maybe my problem will be fixed when adding if (!this.ready()) return; at the top of the hooks to prevent, that the hoock will be do what it should when not be ready. I will test this later and update the status.

// But this just will ( I think so ?) work with onAfterAction and NOT for onBeforeAction.

@cmather
Copy link
Contributor

cmather commented Nov 21, 2014

Can someone post a reproduction I can quickly run and look at? My expectation is that the entire route is run in a computation. But the computation will only rerun if something invalidates it.

@ndemoreau
Copy link

@cmather What do you need exactly as reproduction? I'm having the same issue but I don't know what to send you.

@cmather
Copy link
Contributor

cmather commented Nov 27, 2014

Hi @ndemoreau, a link to a github repository that has code I can download and run. The code should be as simple as possible and demonstrate some unexpected behavior you're seeing. For example, you might have a route with a waitOn option and console.log. Then you would say in a comment, "I'm expecting this to run once but it's running twice". Does that make sense?

In general, all of these functions are run inside a Tracker.autorun which creates a computation that can be invalidated and rerun at any time. So if you have something like a Mongo query inside one of the route functions, or you use this.ready() or any other reactive function, the function can rerun.

@cmather cmather closed this as completed Dec 9, 2014
@advany
Copy link

advany commented Jan 19, 2015

did anyone solf this? i have the same issue...

@advany
Copy link

advany commented Jan 20, 2015

No idea why but multiply:iron-router-progress is causing this. Removed it and the hook isn't called anymore @cmather @ndemoreau

@afareed007
Copy link

i am doing it this nasty way : (any suggestions)
onAfterAction: function () {
if (typeof ranOnceCon == 'undefined') {
console.log("sendingLogMessage");
Meteor.call('sendLogMessage');
ranOnceCon = 1;
}
}

@niranjans
Copy link

@cmather Hi Chris, I have reproduced the error. Here is the Github repo. And you can test the problem here.

Right on the homepage, just open the browser console and you will notice that WaitOn and data are being run twice. When there is no WaitOn, then the data just runs once as normally is expected.

Let me know if I can help.

I also posted a StackOverflow question regarding this.

@abhirakshit
Copy link

I have seen this aswell. Is there some resolution to this?

@stantoncharlton
Copy link

+1

I have also same problem.

waitOn: function() {
            console.log("subscribing");
            return [ Meteor.subscribe('companies')];
    },

subscribing logged twice.

@lorensr
Copy link

lorensr commented Jun 22, 2015

IR hooks are run reactively, so if there are any reactive data sources, like Meteor.subscribe or Meteor.userId(), the hook will be rerun whenever the data source changes (eg becomes ready or gets logged in)

@alochschmied
Copy link

My workaround (for now?) is similar to what @ahmadthd suggested:

onAfterAction: function() {
    if (Tracker.currentComputation.firstRun) {
        // called only once per route
    }
}

@phocks
Copy link

phocks commented Jan 3, 2016

Like @advany I removed multiply:iron-router-progress and the problem of the action being called twice went away. So it seems to be related to that package. The problem is I would like to actually use that package. I might file a bug with them.

Also thanks to @alochschied for if (Tracker.currentComputation.firstRun) works like a charm to only run things once.

@advany
Copy link

advany commented Jan 3, 2016

I switch the flow router

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests