News for hackers. Stuff that matters.
Re:Create a web app from scratch in under 5 minutes with Met... by scribu on Thursday February 07, 2013 @02:44PM (#5184096)

  if(Meteor.isClient) {
    ...
  }


  if(Meteor.isServer) {
    ...
  }
Does the Meteor compiler somehow split those code blocks into separate files, so that it only serves the client code to the browser?

If not, it seems like a pretty bad practice for delivering fast webapps.

Edit: In general, Meteor.isServer seems like a great way for developers to shoot themselves in the foot:

  Dev: Let me just add my $secret in the server-side block...
  Bad guy (viewing source): Oh, what have we here?
  Dev: PWNED :(
Re:Create a web app from scratch in under 5 minutes with Met... by javajosh on Thursday February 07, 2013 @03:00PM (#5184185)
Not sure why you got downvoted; it's a good question. It's not as trivially easy to check as your downvoter might think, as deployed Meteor bundles are minified.

Anyway, the answer is that the entire script is sent to the file and the server. In practice, this isn't a performance issue so much as a security issue - there may be parts of your server code that you don't want clients knowing about.

You can prevent server-side code from getting shipped to the client by placing it in the server subdir.

Re:Create a web app from scratch in under 5 minutes with Met... by wavesounds on Thursday February 07, 2013 @04:48PM (#5184721)
"Does the Meteor compiler somehow split those code blocks into separate files, so that it only serves the client code to the browser?"

Yes.

Re:Create a web app from scratch in under 5 minutes with Met... by javajosh on Thursday February 07, 2013 @05:02PM (#5184807)
Uh, actually, no. And I put together a screencast to prove it.

http://screencast.com/t/RkImuQ9i

[deleted]
Re:Create a web app from scratch in under 5 minutes with Met... by debergalis on Thursday February 07, 2013 @07:08PM (#5185376)
[meteor dev] This is on us. The docs don't make this point clearly enough. It's also the single most voted for thing on the Meteor 1.0 roadmap: https://trello.com/card/patterns-for-writing-larger-modular-...

JS files in the 'server' directory (or any path with a 'server' component') are not sent to the client.

Everything else is.

Putting code inside an `if (Meteor.isServer)` block doesn't keep it from the client. Now someday we might statically analyze the code for clauses like this and keep them out of the client bundle -- the first step is greenspan's jsparse package -- but not likely in the short term.

Re:Create a web app from scratch in under 5 minutes with Met... by substack on Thursday February 07, 2013 @08:28PM (#5185726)
This kind of static analysis is really easy to do with falafel:

https://gist.github.com/substack/4735826

Program:

    var falafel = require('falafel');
    var fs = require('fs');
    var src = fs.readFileSync(__dirname + '/src.js', 'utf8');
     
    var output = falafel(src, function (node) {
        if (node.type === 'IfStatement'
        && node.test.type === 'MemberExpression'
        && node.test.object.name === 'Meteor'
        && node.test.property.name === 'isServer') {
            node.update('');
        }
    });
    console.log(output);
Input:

    if (Meteor.isClient) {
        console.log("I'm the client!");
    }
    
    if (Meteor.isServer) {
        console.log("I'm the server!");
    }
Output:

    if (Meteor.isClient) {
        console.log("I'm the client!");
    }
This won't catch fancy things like Meteor['isServer'] or compound expressions but should be a good place to start.

edit: I had them backwards. Fixed now.

Re:Create a web app from scratch in under 5 minutes with Met... by javajosh on Thursday February 07, 2013 @10:04PM (#5186067)
That's really, really cool and is definitely a good start.