gon icon indicating copy to clipboard operation
gon copied to clipboard

Gon doesn't clear it's data after request

Open d4rky-pl opened this issue 11 years ago • 14 comments

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:

  1. Add gon.push({ :abc => :def }) to FirstController and open it in your browser
  2. 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.

d4rky-pl avatar Feb 07 '14 16:02 d4rky-pl

That is strange. In each request it calls Gon.clear which clear current gon data. Can you check with gon version 5.0.2?

gazay avatar Feb 12 '14 11:02 gazay

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.

d4rky-pl avatar Feb 12 '14 12:02 d4rky-pl

I'm running into this issue as well. Anything I can do to help?

willcosgrove avatar Feb 28 '14 16:02 willcosgrove

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.)

TylerRick avatar Mar 12 '14 23:03 TylerRick

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...

TylerRick avatar Mar 12 '14 23:03 TylerRick

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!

gazay avatar Mar 13 '14 05:03 gazay

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...

TylerRick avatar Mar 31 '14 15:03 TylerRick

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).

eric-smartlove avatar Jun 05 '14 13:06 eric-smartlove

:+1:

ivanovv avatar Aug 12 '14 13:08 ivanovv

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.

seanlinsley avatar Dec 16 '14 18:12 seanlinsley

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.

ludamillion avatar Apr 09 '15 14:04 ludamillion

Any update?

sagarjauhari avatar Aug 14 '15 13:08 sagarjauhari

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.

Zelnox avatar Mar 29 '16 15:03 Zelnox

Same thing here, gon is being retained...

minghz avatar Apr 04 '16 19:04 minghz