akshat
akshat

Reputation: 15922

In Ruby on Rails how can I combine multiple Javascript files at runtime?

I am building a Facebook App which is heavy on Javascript. For this I have multiple Javascript files. Since in Facebook development, the page is served over a tunnel, there is excessive latency added while requesting multiple javascript files. Is it possible to combine the contents of multiple javascript files at runtime? Having multiple files makes it easy to develop, and hence I am avoiding having to combine it while development itself.

Upvotes: 1

Views: 4345

Answers (5)

Kristian
Kristian

Reputation: 21840

in your config file for the environment located at /config/environments, you have a file for each, like development.rb and so on.

In that file, you can set config.assets.debug = false

and that will reduce files down to 1 js file, and 1 css file.

Upvotes: 6

jlfenaux
jlfenaux

Reputation: 3291

Jammit is a good catch too, to combile js and css files.

http://documentcloud.github.com/jammit/

Upvotes: 1

Lummo
Lummo

Reputation: 1169

I've used the asset_packager gem to do this in my applications.

It will combine and minimize your javascript (and CSS) files for production and adds some view helpers that make it very easy to use. There's a rake command that you run to generate the packaged files if the originals have changed.

Upvotes: 0

John Topley
John Topley

Reputation: 115432

You can pass a :cache option to the javascript_include_tag which will combine the files into one:

<%= javascript_include_tag :all, :cache => true %>

Note that this depends on ActionController::Base.perform_caching being set to true, which it is by default for the Rails production environment.

Upvotes: 14

Douglas Mayle
Douglas Mayle

Reputation: 21765

Better than combining at runtime, have a look at Frizione. It's a framework that will allow you to combine your javascript files at deploy time, and compress them. So you get to save all around. It throws in doc generation and JSLint checking as well, but that's just an added bonus.

Upvotes: 1

Related Questions