Search code examples
javascriptmeteoriron-router

Do RouteControllers need to be defined in server code for iron-router@1.0.0-pre3


While attempting to migrate from Meteor 0.8.3 & IR 0.8.2 to Meteor 0.9.3.1 & IR 1.0.0-pre3 I'm running into a problem with RouteControllers.

I have the following js in the '/both' folder of the project:

Router.route('scene.index', {
    path: '/',
    controller: 'SceneController'
});

And the SceneController js in the '/client' tree:

SceneController = RouteController.extend({
    template: 'SceneView'
    ...
});

When attempting to access the route from the client, I get the following error:

Error: RouteController 'SceneController' is not defined.
    at resolve (packages/iron:router/lib/route.js:94)
    at Function.Route.findControllerConstructor (packages/iron:router/lib/route.js:116)
    at Function.Route.createController (packages/iron:router/lib/route.js:134)
    at Function.Router.createController (packages/iron:router/lib/router.js:181)
    at Function.Router.dispatch (packages/iron:router/lib/router_server.js:66)
    at Object.router (packages/iron:router/lib/router.js:15)
    at next (/Users/pward/.meteor/packages/webapp/.1.1.2.1m8ln9s++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:190:15)
    at Function.app.handle (/Users/pward/.meteor/packages/webapp/.1.1.2.1m8ln9s++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:198:3)
    at Object.fn [as handle] (/Users/pward/.meteor/packages/webapp/.1.1.2.1m8ln9s++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:74:14)
    at next (/Users/pward/.meteor/packages/webapp/.1.1.2.1m8ln9s++os+web.browser+web.cordova/npm/node_modules/connect/lib/proto.js:190:15)

After a little quality time with node-inspector, I've come to the conclusion that IR needs client-side RouteControllers to be visible on the server.

My route controllers currently set session variables and I'd prefer not to sprinkle Meteor.isClient around. Is this an IR bug or a known breaking change?


Solution

  • My route controllers currently set session variables and I'd prefer not to sprinkle Meteor.isClient around. Is this an IR bug or a known breaking change?

    You don't need to guard your controller code with Meteor.isClient because it's going to be executed only in the client side even if it's declared in the shared folder.

    Your route definition is better written like this in iron:router@1.0.0-pre3 :

    Router.route('/', {
      name: 'scene.index',
      controller: 'SceneController'
    });
    

    name/path have switched their position.

    EDIT :

    As far as iron:router concepts are concerned, please read this :

    http://eventedmind.github.io/iron-router/#client-and-server

    Having routes defined on both the client and the server is mandatory to determine what needs to be done.

    Client-side, if a link to a client route is detected then we can navigate to it using the HTML5 pushState API, if a server route is detected then standard HTTP communication takes place (for example I use server routes to provide downloadable resources in my app), if the link corresponds to no known path to the router, then an error is triggered.

    Server side, it's important to know about client routes to send 404 on unknown paths instead of serving the Meteor app, here is a quote about this from the official guide :

    It also means that on the server, if there is no client route defined, we can send a 404 response to the client instead of loading up the Meteor application.