Set default 404 error template using IronRouter?

How to define default 404 error template with IronRouter ?
I have set notFoundTemplate, but it is for data not found.
I’m using IronRouter 1.0.7.

I have define “the last route” below, but it’s not work.
It still print ‘Oops, looks like there’s no route on the client or the server’ :

Router.route('/*', {
    name: 'notFound',
    controller: RouteController.extend({
        template: 'notFound'
    })
});

It’s just set notFoundTemplate on Iron-Router configure. Eg, this is my configuration:

Router.configure({
    layoutTemplate: 'layout',
    notFoundTemplate: 'notFound'
});

And this is my template:

<template name="notFound">
    <div class="not-found jumbotron">
        <h2>404</h2>
        <p>Sorry, we can't find nothing in this address.</p>
    </div>
</template>
1 Like

There are two ways to approach this.

  1. General Not found template for invalid route/url: Implementation is just like @allandequeiroz described;
  2. The route is valid but the specified ID, for example, does not exist in the collection.

For the second you can implement like this:
Let’s say you have a Jobs collection:

//Routing code
JobsController = RouteController.extend({
  layoutTemplate: 'standardLayout',
  notFoundTemplate: 'notFound',
  loadingTemplate: 'loading',
  template: 'jobs'
});

Router.route('/jobs/:id', {
  name: 'Jobs',
  controller: 'JobsController',
  waitOn: function () {
    return Meteor.subscribe('jobs');
  },
  onRun: function (){
    if (Jobs.findOne(this.params.id))
      this.next();
    else
      this.render('notFound');
  }
});

Beware that sometimes subscribing to the whole collection could be very slow and heavy to process if the collection is too big, so this is one way to approach it.

Another way would be to Subscribe only to the specific ID passing it as argument in the subscription and then keep the rest the same.

1 Like