Is it possible to make rules for filenames to serve only to client or to server?

Meteor has special directories: server and client for serving files only to server or only to client.

Is it possible to make similar behaviour, but using not client and server folder names but file names which end with either .client.js or .server.js?

For example collections.client.js would be available only on client and collections.server.js would be available only on server?

Maybe if you explain why you want to do this someone can provide a better alternative?

Hi,

I second the request. My use case is this. I already have a bunch of fine-grained folders: folder “author” contains “author.html” and “author.js”, etc.

I don’t want to further clutter my space with adding one-file “client” and “server” subfolders. If my “author” folder contains “author.html”, “client.js” and “server.js” and maybe a couple of other files, that’s the granularity I’d feel most comfortable with.

In fact, I came to this forum looking for a way to hack Meteor to do just that.

Wrap everything inside collections.client.js in a if(Meteor.isClient){ ... } block; and everything in collections.server.js in a if(Meteor.isServer){ ... } block.

You can name your files anything you want to help keep your code organized. You simply need to use the Meteor API to specify which code should be run where.

tl;dr: Yes.

1 Like

That looks like “meteor project organization holywar”. Just create a package and manage everything with meteor package api.

@awatson1978,
This solution does not work like server/ and client/ folders, because server code will be sent to client. So if you have a lot of server code or your server code contains some secure information, then this method fails.

@mrzafod,
Yes, it would work, but also it will force you to manually register all files in your project which is not a pleasure task.

@imkost Yep! This way has some limitation) But I suppose it is better to manage every file in a project and manage dependecies and load order by the hand. I have found this realy clear for building separate UI (templates + styles) for web/cordova or adding new features by api.use/api.imply. Give it a try - it could save the time later

Maybe you are right and I should give it a try. But also it would be nice if Meteor could cut off everything in if (Meteor.isServer) when serving to client.

1 Like

I cant imagine how it could be done. Since a .js file is a scope with variables, contexts and closures it couldnt be cutted just with isClient isServer

@mrzafod, why not? Unless I am missing complexity, I think you just look for

  if (Meteor.isServer) { 
     // Don't send me to the client
  }

that is not inside any curly brackets, and snip it.

(Hmm, now that I think of it, it can be safely snipped even inside curly brackets, no? Care for a counter-example?)

+1 for a filename rule.

Duplicate thread here:

if you create a client and a server folder, anything inside of the server folder only runs on the server, anything in the client folder only runs on client.

if you have a file inside of lib or anything other than server or client, then you could separate the code via Meteor.isServer or Meteor.isClient

Just released imkost:load-order. Now it’s possible (with a little inconvenience) to create your own rules for separating client and server files.

http://github.com/imkost/load-order