jQuery is included twice in Rails application on Heroku

家住魔仙堡 提交于 2020-01-15 11:42:15

问题


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.


回答1:


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.




回答2:


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

?




回答3:


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.



来源:https://stackoverflow.com/questions/12500788/jquery-is-included-twice-in-rails-application-on-heroku

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!