Kadirahq/flow-router - "redirect needs to be done in sync" error

Hi All

I implemented some redirects in flow-router that worked fine in development but not in production. I got the same in development when I run with --production.

So it’s a compilation issue. I thought I’d alert it here as it’s a little bizarre and worrying as the JS minifier seems to be a tad over-zealous (incorrect!). The code responsible is in flow-router/client/triggers.js:

//  run triggers and abort if redirected or callback stopped
//  @triggers - a set of triggers 
//  @context - context we need to pass (it must have the route)
//  @redirectFn - function which used to redirect 
//  @after - called after if only all the triggers runs
Triggers.runTriggers = function(triggers, context, redirectFn, after) {
  var abort = false;
  var inCurrentLoop = true;
  var alreadyRedirected = false;

  for(var lc=0; lc<triggers.length; lc++) {
    var trigger = triggers[lc];
    trigger(context, doRedirect, doStop);

    if(abort) {
      return;
    }
  }

  // mark that, we've exceeds the currentEventloop for
  // this set of triggers.
  inCurrentLoop = false;
  after();

  function doRedirect(url, params, queryParams) {
    if(alreadyRedirected) {
      throw new Error("already redirected");
    }

    if(!inCurrentLoop) {
      throw new Error("redirect needs to be done in sync");
    }

    if(!url) {
      throw new Error("trigger redirect requires an URL");
    }

    abort = true;
    alreadyRedirected = true;
    redirectFn(url, params, queryParams);
  }

  function doStop() {
    abort = true;
  }

};

This seems to transpile down to:

h.runTriggers = function(t, e, r, n) {
    for (var i = !1, o = !0, a = !1, s = 0; s < t.length; s++) {
        var u;
        if ((0,
        t[s])(e, c, p),
        i)
            return
    }
    function c(t, e, r) {
        if (a)
            throw new Error("already redirected");
        throw new Error("redirect needs to be done in sync")
    }
    function p() {
        i = !0
    }
    o = !1,
    n()
}

Which does not reflect the spirit of the original code. It seems the transpiler is assuming that inCurrentLoop is always false within the doRedirect function!

I moved these two lines to the bottom of the outer function (below the last two inner functions) and then it works as expected:

  inCurrentLoop = false;
  after();

Am I missing something or is this a blatant error in the transpiler? How come this is not affecting a gazillion other bits of code?

1 Like

I ran into the same problem: Minification issue with flow-router · Issue #11756 · meteor/meteor · GitHub

Yes, our recommendation is to use ostrio:flow-router-extra instead.

1 Like

What happens if we have the kadira:flow-router installed together with arillo:flow-router-helpers?

Is the recommendation to remove both and only use ostrio:flow-router-extra?

Are the any code changes necessary or only if we want to use the added functionality (the extra part?)?

After removing kadira:flow-router and installing ostrio:flow-router-extra instead I get the following error:

Please remove `zimme:active-route` package, as its features is build into flow-router-extra, and will interfere.
b7e70df4312a83aa8c45c42106ddfcf58760b1f4.js:1 meteor remove zimme:active-route
b7e70df4312a83aa8c45c42106ddfcf58760b1f4.js:1 Please remove `arillo:flow-router-helpers` package, as its features is build into flow-router-extra, and will interfere.
b7e70df4312a83aa8c45c42106ddfcf58760b1f4.js:1 meteor remove arillo:flow-router-helpers
b7e70df4312a83aa8c45c42106ddfcf58760b1f4.js:166 Uncaught Error: Uncaught Error: Duplicate ReactiveDict name: activeRouteConfig:172:meteor://desktop/__cordova/b7e70df4312a83aa8c45c42106ddfcf58760b1f4.js?meteor_js_resource=true

So I followed the this and removed arillo:flow-router-helpers which in turn also removed `zimme:active-route".

However upon restarting the frontend app I get the same redirect needs to be done in sync error. Am I the only one with this error?

We just migrated from kadira:flow-router to ostrio:flow-router-extra and are also still seeing the redirect needs to be done in sync error. We removed arilla:flow-router-helpers and zimme:active-route as well. Curious if you were ever able to resolve this.

Yes, it turned out that there were one or more import statements in the code directly. After removing them all worked fine. So check that your packages are also gone from your source code, not just the package.json and Meteor’s packages file.

I had to update useraccounts:core to remove useraccounts:flow-router as a dependency and then add useraccounts:flow-router-extra as you detailed here. Thanks for the help!