# Newbie: what is preferred approach for including javascript files?

**URL:** <https://www.ruby-forum.com/t/newbie-what-is-preferred-approach-for-including-javascript-files/191443>\
**Category:** Rails\
**Created:** [July 12, 2010, 7:44pm UTC](https://www.ruby-forum.com/t/newbie-what-is-preferred-approach-for-including-javascript-files/191443 "2010-07-12T19:44:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@Lille](https://www.ruby-forum.com/u/Lille)\
**Post date:** [July 12, 2010, 7:44pm UTC](https://www.ruby-forum.com/t/newbie-what-is-preferred-approach-for-including-javascript-files/191443/1 "2010-07-12T19:44:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Lille](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f4b2a3/32.png) [@Lille](https://www.ruby-forum.com/u/Lille)\
**Post date:** [July 13, 2010, 12:25pm UTC](https://www.ruby-forum.com/t/newbie-what-is-preferred-approach-for-including-javascript-files/191443/2 "2010-07-13T12:25:03Z")

</div>

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](http://guides.rubyonrails.org/layouts_and_rendering.html#asset-tags). 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.
