gon
gon copied to clipboard
Gon doesn't clear it's data after request
Using
gon.push({ :abc => :def })
causes gon.abc to be available between requests, just like something gon.global would do according to docs.
Steps to reproduce:
- Add gon.push({ :abc => :def }) to FirstController and open it in your browser
- Open SecondController and see that include_gon returned
window.gon={};gon.abc="def";
This happens on Windows using Ruby 1.9.3 and thin. The solution is to use gon.clear
in before_filter for every controller, but that is far from being convenient.
That is strange. In each request it calls Gon.clear which clear current gon data. Can you check with gon version 5.0.2?
I already use 5.0.2. It seems like Thread.current('gon') returns Gon object with all variables present. I'll investigate more later and let you know what I find.
I'm running into this issue as well. Anything I can do to help?
This problem seems to occur if a previous request used gon but the current request does not explicitly use gon: It will just reuse the data that was set in the previous request.
"In each request it calls Gon.clear which clear current gon data." This is not quite accurate, I'm afraid. Where does it get called from? I can't find anywhere that guarantees that it will get reset every request.
If gon
gets called anywhere at all in the new request, then it appears that things get reset (though not via Gon.clear
, by the looks of it), because wrong_gon_request?
returns true:
def gon
if wrong_gon_request?
gon_request = Request.new(env)
gon_request.id = request.uuid
Thread.current['gon'] = gon_request
end
Gon
end
but if gon
doesn't get called by some controllers/actions, then it doesn't end up getting reset.
A workaround, like @d4rky-pl mentioned, is to add a before_filter -> { gon.clear }
in your controller. But even just adding a call to gon
seems to be enough trigger it to reset, as discussed above.
This before_filter could even be added automatically when GonHelpers
is included into ActionController::Base
... Though I think there are better ways of doing it...
I really liked the way the request_store
gem does this. They have a middleware that automatically clears their request store (which is also stored in a thread-local variable, as in this gem) before every request. What would you think of doing the same thing in gon
and adding a Gon middleware?
(Their middleware automatically gets inserted into the stack here in the Railtie.)
We should probably also add an integration test to the test suite that exposes this bug and then proves it is fixed... It could request an action that uses gon, and then another action that doesn't, and then inspect the script tag in the rendered page to make sure it doesn't have any gon variables set...
Thank you! Yes, I think one of possible solutions is to create middleware like in request_store. But also I think we can simply check wrong_gon_request?
in include_gon
helper here - https://github.com/gazay/gon/blob/master/lib/gon/helpers.rb#L11. And clear it if it is wrong. What do you think?
It would be awesome if you can write this integration test!
Yeah, I think if we could somehow check wrong_gon_request?
from include_gon
that would probably work. I think I may have tried playing around with that idea and found that I couldn't access wrong_gon_request?
from there, because wrong_gon_request?
(GonHelpers
controller helpers) is only available from the controller.
Probably won't get a chance to help with this since I'm no longer working on the project that used this...
If anyone else who encounters the same problem, I think it is useful to note that the problem related in this ticket seems to have been fixed in ed28c12e7cf134f6918f4742ba0d27d7568e5ab0 which is available in master
branch since b34d796b5f5beb1bb296368935e5f6d0ed4a67e1, although it has not been published yet.
However, you can still encounter similar problems during controller tests as reported in #124: gon is not reseted between tests when running rake test:controllers
, for instance (I use minitest in my case).
:+1:
I was running into this issue in a controller test that's using render_views
. Gon was referencing some records from a previous test created by FactoryGirl, that had since been deleted. Adding a before_filter
as suggested gets around this.
Any updates on this? We've just run afoul of this issue in our tests. The above workaround works fine but seems like it shouldn't be necessary.
Any update?
I'm getting this problem after updating to 6.0.1 from 4.0.x. I also have to clear gon
in a before_action
now.
Same thing here, gon
is being retained...