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?