Skip to content

"If you load both jquery_ujs and rails-ujs, use rails-ujs only." when rails-ujs imported twice #40584

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

Closed
tai2 opened this issue Nov 9, 2020 · 5 comments
Labels

Comments

@tai2
Copy link

tai2 commented Nov 9, 2020

Steps to reproduce

You use two JS bundles on a page and both of them import rails-ujs.
In addition to that, that page exposes jQuery in the global space.

// bundle_a.js
import { csrfToken } from 'rails-ujs'
...
// bundle_b.js
import { csrfToken } from 'rails-ujs'
...
<!-- page.html.erb -->
<script src="jquery-3.5.1.min.js"></script>
<script src="bundle_a.js" />
<script src="bundle_b.js" />

Expected behavior

  • No runtime error
  • You can import rails-ujs in each JS bundle
  • You can use csrfToken() in each JS bundle

Actual behavior

page.html.erb crush with a runtime error which says 'If you load both jquery_ujs and rails-ujs, use rails-ujs only.

The line throws this error is here:

if jQuery? and jQuery.ajax?
throw new Error('If you load both jquery_ujs and rails-ujs, use rails-ujs only.') if jQuery.rails
jQuery.rails = Rails

System configuration

Rails version:
5.2.4.3

Ruby version:

2.6.5

@tai2 tai2 changed the title "If you load both jquery_ujs and rails-ujs, use rails-ujs only." when 'rails-ujs` imported twice "If you load both jquery_ujs and rails-ujs, use rails-ujs only." when rails-ujs imported twice Nov 9, 2020
@tai2
Copy link
Author

tai2 commented Nov 9, 2020

I guess moving these 5 lines under Rails.start function mitigates this problem.

if jQuery? and jQuery.ajax?
throw new Error('If you load both jquery_ujs and rails-ujs, use rails-ujs only.') if jQuery.rails
jQuery.rails = Rails
jQuery.ajaxPrefilter (options, originalOptions, xhr) ->
CSRFProtection(xhr) unless options.crossDomain

However, this change corrupts apps that uses jQuery.rails without calling Rails.start.

@tai2
Copy link
Author

tai2 commented Nov 9, 2020

This behavior was from 9df747c

@tahsin352
Copy link
Contributor

I remove the jquery and its working properly

<%= javascript_pack_tag 'bundle_a' %>
<%= javascript_pack_tag 'bundle_b' %>

@tai2
Copy link
Author

tai2 commented Nov 12, 2020

Yes, of course. It means you cannot use rails-ujs with jquery(or you can use it a limited way).

@rails-bot
Copy link

rails-bot bot commented Feb 10, 2021

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 6-1-stable branch or on main, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot added the stale label Feb 10, 2021
@rails-bot rails-bot bot closed this as completed Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants