Issue:
I have an E-commerce application built on Spree 3.7 and Rails 5.2, Right now I am facing turbolink(version 5.2.0) issue in which I have implemented javascript for adding items to the cart but js method firing twice and a similar item is being added into cart twice.
I have included following js in head tag-
<script>
<%= javascript_include_tag 'spree/frontend/all' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</script>
I have tried 'data-turbolinks-track': 'true/false' but my issue solved when removed
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
.
I read about turbolink from 'https://github.com/turbolinks/turbolink'. According to me turbolink is loading javascript method twice.
Any suggestions on how it can be solved?
After comment here is my application.js file
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require activestorage
//= require turbolinks
//= require cable
//= require jquery-ui/widgets/sortable
//= require wishlist
//= require product
//= require cart
So basically I was able to solve the issue, according to Turbolink our javascript should be wrapped in
document.addEventListener("turbolinks:load", function () { ... }
Our spree functions like addToCartForm etc. works on $( document ).ready()
So I have overridden those functions and wrapped in turbolinks:load
, now everything is working fine.