简体   繁体   中英

Rails asset pipeline: JavaScript being executed twice

My JavaScript is being executed twice. When I run the Chrome debugger, I noticed that the individual JavaScript file will execute the code the first time, then after that, the application.js file will execute the code again.

I believe this is happening because I recently turned precompile off because I was having issues with Heroku. After this I ran rake assets:precompile .

Turbolinks

You probably have an issue with Turbolinks (considering you've included it in your asset pipeline); although this might not be the problem.

Turbolinks is meant to speed up your page load times by loading the <body> tag of your new pages with ajax , keeping the <head> tag intact. This is meant to let your application load just the HTML it needs, but messes up the JS on your page.

If you're having your JS fired twice, it could be a sign that Turbolinks is interfering with your JS. Because you haven't included any examples from the logs , I won't be able to give you any specific information, but you should be able to test this by removing the turbolinks include from both your layout and your application manifest JS file:

#app/views/layouts/application.html.erb
= javascript_include_tag "application", "data-turbolinks-track" => false

#app/assets/javascripts/application.js
//= require turbolinks -> remove this line

If you do this, restart your server & reload the page in question. If it still comes back with the erroneous functionality, it means Turbolinks is not the problem (and you should reset all the things you changed)

--

Events

The other problem you might have is how you're calling / triggering your events. This could be down to a number of reasons, but you should definitely look at how you're calling your JS:

#app/assets/javascripts/application.js
$(document).on("click", ".element", function(){
    //your stuff here
});

We always delegate from the document element of the DOM now. Whilst this might cause some resource issues, we've found it makes Rails apps much more robust with the Asset Pipeline, as it allows you to truly identify elements on the page.

If you're trying to trigger an event from a single object on your DOM, you may find that JS will "capture" the event twice (for whatever reason), leading to your issue. Try delegating your events which are being called twice

--

Functions

Finally, your functions could be the issue.

If you have any anonymous functions , or functions for specific events, you will need to ensure they are able to be called correct. You should do this by using page events in JS & Turbolinks :

#app/assets/javascripts/application.js
var your_function = function() {
    //stuff here
};

$(document).on("page:load ready", your_function);

I had this same problem on a rails 3 app that is not using turbolinks. I was able to fix it by running:

rake assets:clean

This link helped me: Jquery Rails 3... form submits twice... deletes twice... help

I experienced this issue because of the asset pipeline including both jquery, jquery-ujs, and rails-ujs:

 //= require jquery //= require jquery_ujs //= require rails_ujs //= require bootstrap //= require turbolinks //= require_tree . 

Removed rails_ujs and all was fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM