0

Rails version 5.1.3

Specifically, it's the code in assets/javascripts/channels/my_channel.coffee where channel js client code lives, with other general js code. I'm using actioncable. But general jQuery/javascript callbacks suffer from this, too. Rewriting jQuery append in javascript didn't make difference.

All the callbacks are triggered twice. In other words, the fucntions to be executed after callbacks are triggered are called twice, including channel subscription and $(document).ready. Concretely, if I put

$(some_id_name).append some_string

In the event function in $(document).ready, it will be appended twice. I consider actioncable channel subscription instance as a callback, too. The receive method is called by user sending messages to trigger the instance. So the code in that method is executed twice, too. append in my case.

Could be entire js file is loaded twice. But how?

This only happens in test and production environment(Heroku), not in development environment. Implies anything?

I removed turbolinks cleanly in 3 steps as you can find by searching online. Include jquery and rails-ujs exactly once, no jquery_ujs, in application.js. No public/assets exists.

Part of my_channel.coffee looks like the following. All custom functions contain several callbacks like click, keydown. If I use unbind and bind to them it will solve the problem. But I can't apply this to $(document).ready and channel subscription instance.

$(document).ready ->
  $('#messages').append "document is ready"
  submitQuestion()
  submitAnswer()
  hideJudgeForm()
  return

In application.js

//= require jquery
//= require rails-ujs
//= require bootstrap
//= require spin.js/spin
//= require_tree .
Xiaohong Deng
  • 135
  • 2
  • 12
  • Can you make sure that the files are not being included twice in the final HTML generated? In practice, can you press F12 in the browser and analyze it? – André Guimarães Sakata Oct 10 '17 at 22:29
  • @AndréGuimarãesSakata Yes I double checked `<%= javascript_include_tag 'application' %>` in `application.html.erb` and nowhere else should have it. Sorry I'm not sure how to analyze double loading in browser console. – Xiaohong Deng Oct 10 '17 at 22:35

1 Answers1

2

Well, the very first thing I would do is to check that I don't have any other file including the file my_channel.coffee. Neither as part of another bundle nor with javascript_include_tag.

After that, I would try removing the require_tree . in all my files and replace it with the specific list of files that I want Rails to load as part of the bundle. You might have two different files importing my_channel.coffee indirectly because of this.

damianmr
  • 2,511
  • 1
  • 15
  • 15
  • removing `require_tree .` actually works. Currently I hold the opinion that `require_tree folder` requires both `my_channel.coffee` and `my_channel.js` in folder `channels`. I thought assets pipeline didn't include coffee files. Or does it? Who can confirm? Anyway, by requiring only js files it works. – Xiaohong Deng Oct 10 '17 at 23:09
  • Also for what reason development env doesn't suffer from this? If it's bc it doesn't use precompile then how it accesses `my_channel.coffee` or `my_channel.js`. – Xiaohong Deng Oct 10 '17 at 23:13
  • Sorry I cannot give you a proper answer, last time I used Rails it was in version 4. But back in my time I never used a `.coffee` extension directly. I would name my files something like `my_channel.js.coffee.erb` (the `erb` part can be omitted if you don't need any Rails helper) so that the Assets pipeline would parse the file first as an ERB, then as a coffee and lastly as a JS. In your case, you are probably creating, indirectly, two `my_channel.js`: one because you already created an actual `my_channel.js` file and the other because of the compilation of `my_channel.coffee` – damianmr Oct 11 '17 at 10:54
  • Good enough. It is a plugin I use which compile coffee on save. I thought it was rails doing so I assumed coffee wouldn't be compiled. – Xiaohong Deng Oct 12 '17 at 00:40