PSA: Using iron-router? Make sure to test your app in Win10/Microsoft Edge

Hi Forum!

Stumbled upon an edge case today, in… Microsoft Edge, which only manifests if you’re using RouteController's in a specific way.

The annoying part- The error is only thrown when you navigate to the affected route!

This means you need to test your app by going to every route!

Or, you can try understand the problem, and see if the issue applies to you-

// This example works in Chrome/Firefox, but fails in Edge.
var A = RouteController.extend({
  // In Edge this function gets the name `onBeforeAction`, even though it's "anonymous"
  // Where Chrome/Firefox, the name == ""
  onBeforeAction: function() {
    console.warn('A');
    this.next();
  }
});
var B = A.extend({
  // Again, this function also gets the name `onBeforeAction`
  onBeforeAction: function() {
    console.warn('B');
    this.next();
  }
});
Router.route('extendTest', {
  controller: B
});
// Only when you `go` you'll get the error:
//  "Handler with name 'onBeforeAction' already exists."
// Because MiddleWareStack is fussy about having uniquely named functions
Router.go('extendTest');

The Fix?

  1. Change your code to ensure they stay “anonymous” functions.
// By defining it outside an object literal, it remains anonymous
var onBeforeAction = function(){}
console.assert(onBeforeAction.name == "")

var A = RouteController.extend({
  onBeforeAction: [
    // By wrapping in an array, we avoid Edge's auto-naming.
    function() {
      console.warn('A');
      this.next();
    }
  ]
});
  1. OR, Try out the the code from this PR
2 Likes

Hi Nathan,

I tested my app on Edge and have a very similar problem. The error I get is:

Exception in callback of async function: Error: Handler with name ‘onRun’ already exists.
at MiddlewareStack.prototype._create

Do you think it’s the same issue? Should I wrap the onRun call in my controller in an array?

The same is happening in the latest version of Chrome Canary (currently v50.0.2653.0 but it started in v50.0.2651.0 I believe).

Thank you for investigating and fixing it!

@miro on what platform are you using Chrome Canary? Windows? I tested on MacOSX and have no issue.

Yep, I was getting the same error for an onRun hook.

Wrapping it in an array is a quick fix, just make sure you leave a comment so it doesn’t get refactored out!

@miro / @dtamburr I was able to reproduce in Chrome Canary (on OSX), but only with harmony flag enabled - chrome://flags/#enable-javascript-harmony

1 Like

@dtamburr on MacOSX - went back to v50.0.2646: no problems so far;

@nathan_muir you’re right - I have harmony enabled.

Well, at least It seems they are actively working on ES6 :wink:

I found out that in my case I only get this error when testing locally (accessing with http://localhost:3000) and not on my test server (accessing the app with http://myserver.com). Anyone else experiencing this or do you have this behavior all the time?

It borked me both local and online (on the same code).

It seems that in the latest Chrome Canary (v51.0.2670.0) they have moved some features to stable harmony branch (#disable-javascript-harmony-shipping), so disable Latest stable JavaScript features as well.

@miro Mine only crashed online.
Disabling ‘Latest stable Javascript features’ solved it, thanks for the tip.
I don’t have to waste time looking for something wrong in my code

Fix was made on the 24th march:

Fixed: meteor update iron:middleware-stack.