Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include javascript file only once?

I am using pace.js library and its documentation says, that I need to include their JS/CSS file as early as possible. http://github.hubspot.com/pace/ . I did it and now my application.html.erb looks this way:

<head>
  <%= javascript_include_tag "pace.min" %>
  <%= stylesheet_link_tag "pace", :media => "all" %>

  <title>My site</title>
  <%= csrf_meta_tags %>
  <%= stylesheet_link_tag "application", :media => "all" %>
  <%= javascript_include_tag "application" %>
</head>

But when I look at generated HTML I can see that pace is being included twice. First time at the begining of <head> and second time in place of javascript_include_tag "application" How to prevent it?

like image 781
yaru Avatar asked Apr 12 '26 07:04

yaru


1 Answers

First time pace.js is included because of <%= javascript_include_tag "pace.min" %> statement where you have explicitly specified to include pace.js library.

And second time because of the following statement in application.js

//= require_tree .

This statement means that include all the js files in the current directory of application.js(app/assets/javascripts) and I am guessing that you have kept pace.js in the same directory which is why it is being included again.

EDIT

Use stub directive in application.js, if you want to exclude pace.js from the asset bundle and include it explicitly in <head>. This way pace.js would only be included once.

//= stub pace

like image 200
Kirti Thorat Avatar answered Apr 13 '26 21:04

Kirti Thorat