0

I've read and tried the solutions here (Ruby On Rails 3.1 - assets pipeline - assets rendered twice) for the duplicate assets problem in Rails, but it's not working on my scenario, which looks like this:

Layout:

<!DOCTYPE html>
<html>
<head>...</head>
<body>
...
<!-- including :application -->
<%= javascript_include_tag :application %>
<!-- yielding :body_script -->
<%=  yield :body_script %>
</body>
</html>

Then, in a "users" view:

<%= content_for :body_script do %>
    <%= javascript_include_tag 'users' %>
<% end %>
...
...
...

My javascript dependencies are that:

  • application.js "requires" jquery, bootstrap and self
  • users.js "requires" worker_interface.js
  • worker_interface.js "requires" jquery, knockout and application

When the "users" view is rendered, I'm getting duplicate jquery and application.js on the end of my body, one of each being rendered because of the javascript_include_tag :application and the other because of yield :body_script, like this:

<!-- including :application -->
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/bootstrap.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<!-- yielding :body_script -->
<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/knockout.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
<script src="/assets/worker_interface.js?body=1" type="text/javascript"></script>
<script src="/assets/users.js?body=1" type="text/javascript"></script>

I know that I have redundant dependencies here, because I am asking to include jquery and application more than once (indirectly). However, shouldn't Rails guarantee that no duplicate files are rendered? Can I still keep the "redundant" dependencies and have the files rendered only once?

BTW, I'm using Rails version 3.2.9.

Community
  • 1
  • 1
fegemo
  • 2,475
  • 6
  • 20
  • 40
  • Do you use `require_tree` in your application.js? People have told me that Rails is smart enough not to include a given asset more than once but I've seen exactly the opposite time and time again. The asset pipeline can be a mysterious thing. Another thing worth looking at is if you have some assets that are already precompiled. I notice that sometimes my development environment will load both non-static and static assets, might be a config setting. – Noz Feb 19 '13 at 17:04
  • Hey @Cyle, I'm not using require_tree anywhere. And I have disabled the serving of static assets with "config.serve_static_assets = false" :( – fegemo Feb 19 '13 at 17:27
  • I'm assuming that sadface means it didn't work ;) . You restarted your server after changing that setting right? Also most modern browsers will cache old CSS if you're testing locally. Try clearing your cache. If that doesn't solve it then the only other thing I can suggest is to checkout out the blank line answer suggested in this SO http://stackoverflow.com/questions/8558472/javascript-included-twice-in-a-rails-3-1-asset-based-app – Noz Feb 19 '13 at 17:38

1 Answers1

2

shouldn't Rails guarantee that no duplicate files are rendered? Can I still keep the "redundant" dependencies and have the files rendered only once?

simple answer: no

assets are precompiled and so "static" in production. you may include assets in several ways. so there is no way for rails to "know" what files should be eliminated in the context of your rendered page.

YOU need to make sure there are not duplicates.

phoet
  • 18,688
  • 4
  • 46
  • 74
  • From front-end dev perspective this is the exact reason that #=require is not a satisfactory solution for loading JS files in any JS app with even slight complexity. Use a module loader. – Pure Function Jun 27 '15 at 13:03