lanan
lanan

Reputation: 2752

jQuery is included twice in Rails application on Heroku

My question is similar to some other questions here which still didn't provide an answer for my situation

I am including jQuery gem in my app in Gemfile:

gem 'jquery-rails'

And in application.js:

//= require jquery
//= require jquery-ui
//= require jquery_ujs
...
//= require_tree .

My development.rb has

# Do not compress assets
config.assets.compress = false
# Expands the lines which load the assets
config.assets.debug = true

My assets are not precompiled, there is no public/assets. Everything is running great locally. But when I push to heroku I get different application.js file included on the page. Instead of being a manifest file it has minified version of jQuery. Therefore I get jQuery included twice. Here is how source of my page looks like:

<script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery-ui.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
...
<script src="/assets/application.js?body=1" type="text/javascript"></script>

And when I click on /assets/application.js?body=1 I see minified jQuery. When I run my app locally and click on /assets/application.js?body=1 I just see the manifest. I tried to add to development.rb

 config.serve_static_assets = false

It didn't help. I also cleared assets and cache multiple times. The only thing that works for me is setting

config.assets.debug = false

But I would like to know if there is any other option that won't affect debugging.

Upvotes: 1

Views: 1184

Answers (3)

Michael Durrant
Michael Durrant

Reputation: 96484

Development and Production and supposed to be different (/app/assets and not minified in development, public/assets and minified in production.

As for the double-include, if your production web served page has both, I would check your app for pages like application.html.erb to see if if it's defined there. If so, remove it.

Upvotes: 0

Taimoor Changaiz
Taimoor Changaiz

Reputation: 10684

I removed this line

//= require_tree .

and creating a sub folder in javascripts and then moved all js files in javascript directory to that subfolder except application.js

and wrote this line

//= require_tree ./sub_folder_name

It's awsome and working.

One Important thing to Note:

This may cause to you as you are including a same file twice one manually by javascript_include_tag and other by application.js. First confirm that if this is the case you have to include it once.

Upvotes: 1

jakeonrails
jakeonrails

Reputation: 1895

Take a look at this article on heroku regarding precompiling your assets. When you push to heroku do you see any error messages around the section that says:

-----> Preparing Rails asset pipeline
       Running: rake assets:precompile

?

Upvotes: 0

Related Questions