How do I redirect all non-existent routes in iron:router?

With iron:router I’m basically just trying to make a global onBeforeAction that redirects to index. So I don’t want a notFoundTemplate to render but rather a full redirect, but I can’t seem to get it to work. What I’m doing now is:

var goToSplash = function() {
  Router.go('index');
  this.next();
};

Router.onBeforeAction(goToSplash);

I’m assuming that it’s because of the this.next() but removing that yields the classic error of:

Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?

Any tips?

Just add one more route at the last position catching all non-matched paths.

1 Like

Thanks for the tip! For people wanting to setup a catchall this is from the iron-router docs (which I seemed to miss):

Iron Router now uses path-to-regexp, which means the syntax for catchall routes has changed a little -- it's now '/(.*)'.

Eg. in my case:

Router.route('/(.*)', {
  action: function() {
    Router.go('index');
  }
});
2 Likes