Newbie: what is preferred approach for including javascript files?

Hi,

What is the cost of not cacheing javascript? If that cost is serious,
do I prefer to selectively include javascript files only for view
where they are needed? In other words I see four options:

  1. Cache everything in layouts/application.html.erb and don’t worry
    about selective inclusion of .js files where they are needed;
  2. cache some things in (e.g., dependencies like jquery) and
    selectively include the rest only where they are needed;
  3. do not cache and only include javascript in the views it is needed;
  4. cache nothing and include everything in layouts/
    application.html.erb.

I cobbled together javascript functionality in development mode, such
that my layouts/application.html.erb file came to look like this:

... <%= javascript_include_tag :defaults, "jquery-1.4.2.js", :cache=>true %> <%= javascript_include_tag "jquery.maskedinput-1.2.2.js", :cache=>true %> <%= javascript_include_tag "autoNumeric-1.5.1.js", :cache=>true %> <%= javascript_include_tag "autoLoader-1.5.1.js", :cache=>true%> <%= javascript_include_tag "jquery.metadata.js", :cache=>true %> ...

When, recently, I switched into production mode, I saw that the
expected javascripts/all.js cache file had been created, but,
unfortunately, a lot of my javascript functionality was knocked out.

When I remove the :cache option from my layouts/application.html.erb
file, like this…

... <%= javascript_include_tag :defaults, "jquery-1.4.2.js" %> <%= javascript_include_tag "jquery.maskedinput-1.2.2.js"%> <%= javascript_include_tag "autoNumeric-1.5.1.js" %> ...

…I get back a lot of my functionality.

Thanks,

Lille

Lille wrote:

Hi,

What is the cost of not cacheing javascript?

Slower retrieval of the included files.

If that cost is serious,
do I prefer to selectively include javascript files only for view
where they are needed? In other words I see four options:

Layouts and Rendering in Rails — Ruby on Rails Guides. See
3.1.2 - Linking to Javascript files.

  1. Cache everything in layouts/application.html.erb and don’t worry
    about selective inclusion of .js files where they are needed;
  2. cache some things in (e.g., dependencies like jquery) and
    selectively include the rest only where they are needed;
  3. do not cache and only include javascript in the views it is needed;
  4. cache nothing and include everything in layouts/
    application.html.erb.