Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jQuery is included twice #5095

Closed
dnagir opened this issue Feb 20, 2012 · 11 comments
Closed

jQuery is included twice #5095

dnagir opened this issue Feb 20, 2012 · 11 comments

Comments

@dnagir
Copy link
Contributor

dnagir commented Feb 20, 2012

In development mode the jquery is included twice: 1st time as a separate file /assets/jquery.js and then embedded into /assets/application.js.

This effectively makes it impossible to properly use jquery and all the related stuff in development environment.

Repro:

> rails -v
Rails 3.2.1

> rails new jq2
# Wait for stuff to be bundled...

> cd jq2/
> rails s -d
> curl "http://localhost:3000/assets/jquery.js?body=1" | head -n 2
/*!
 * jQuery JavaScript Library v1.7.1

> curl "http://localhost:3000/assets/application.js?body=1" | head -n 2
/*!
 * jQuery JavaScript Library v1.7.1

The real problem seems to be that the application.js expands the jquery.js and includes it into the page BUT doesn't exclude it itself.

As a result jQuery is loaded twice (from jquery.js and application.js):

> rails g scaffold pages
> bundle exec rake db:migrate
> rails s -d

> curl http://localhost:3000/pages | grep ".js"

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

I 'm not sure why it hasn't been reported before (or has it?) as it seems to break a lot of things.

@adrianpacala
Copy link
Contributor

That's the way Sprockets works. If you include body=1 (which is default in development) in the URL then the response won't include the files listed in your manifest (e.g. the jquery.js). In your first example you're curling application.js without the body=1 and that's why you get jQuery code inside.

@dnagir
Copy link
Contributor Author

dnagir commented Feb 20, 2012

That would be fine, but unfortunately I can clearly see the same output even when ?body=1 is included.
(will update the original issue to avoid the confusion).

@kennyj
Copy link
Contributor

kennyj commented Feb 20, 2012

@dnagir

Please tell us some files in your public/assets directory :-)
If there is public/assets/application.js, it seems that this issue is reproduced.

@dnagir
Copy link
Contributor Author

dnagir commented Feb 21, 2012

@kennyj there is no public/assets directory since it is a brand new application:

> tree public/
public/
├── 404.html
├── 422.html
├── 500.html
├── favicon.ico
├── index.html
└── robots.txt

@kennyj
Copy link
Contributor

kennyj commented Feb 21, 2012

Umm...
When I tested a brand new application yesterday, I could reproduce this issue !
But it seems that body=1 is important factor.

$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-linux]
$ rails -v
Rails 3.2.1
$ rails new jq2
...
$ cd jq2/
$ rails s -d
$ curl "http://localhost:3000/assets/jquery.js?body=1" | head -n 2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0
/*!
 * jQuery JavaScript Library v1.7.1

■ with body=1
$ curl "http://localhost:3000/assets/application.js?body=1" | head -n 2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   585  100   585    0     0  22364      0 --:--:-- --:--:-- --:--:--     0
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.

■ without body=1
$ curl "http://localhost:3000/assets/application.js" | head -n 2
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0  257k    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
/*!
 * jQuery JavaScript Library v1.7.1

It it odd...
BTW, My Gemfile.lock is https://gist.github.com/1874619 .

@dnagir
Copy link
Contributor Author

dnagir commented Feb 21, 2012

That is strange. I definitely can see the jquery duplicated.

Here's my Gemfile.lock

@dnagir
Copy link
Contributor Author

dnagir commented Feb 21, 2012

There is slight difference in the Gems, but doesn't look like related to this issue at all:

> diff Gemfile.lock Gemfile.lock.kennyj 
31c31
<     arel (3.0.0)
---
>     arel (3.0.1)
45c45
<     journey (1.0.1)
---
>     journey (1.0.2)

@batter
Copy link

batter commented Feb 22, 2012

I'm also seeing this occur using rails 3.2.0. Trying to figure out why, but it seems to be causing a javascript error to occur due to the fact that Jquery is being re-initialized after multiple scripts dependent upon it have already been included.

It seems to ignore the directive of config.assets.compress = false, and compress them into one application.js file anyways and add them to the file.

Note: this doesn't occur if you turn assets debug off as described here in the asset docs.

@batter
Copy link

batter commented Feb 23, 2012

So I found a temporary workaround after finding this post on stackoverflow. If you add config.serve_static_assets = false to config/environments/development.rb it seems to properly acknowledge the config.assets.compress = false directive (and serve your raw application.js file as opposed to the compressed version) if and only if you have run rake assets:precompile.

If you haven't run rake assets:precompile at least once, and your project does not have a file located at public/assets/application.js, then this workaround doesn't work.

So I'm fairly certain there's a bug where it doesn't acknowledge config.assets.compress = false properly for some reason. Hopefully this can be fixed in the next release of rails 3.2?

@batter
Copy link

batter commented Feb 23, 2012

I have submitted a new issue ticket with a condensed description, and what I feel is a more appropriate title in the hopes that it will be easier for someone to respond to. It has been filed at Issue 5145.

@vijaydev
Copy link
Member

vijaydev commented Mar 7, 2012

Closing this in favor of #5145

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants