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?