nikolalsvk / render_async

render_async lets you include pages asynchronously with AJAX
https://rubygems.org/gems/render_async/
MIT License
1.08k stars 75 forks source link

Toggle & Turbolinks - turbolinks:load event forces _renderAsyncFunction to fire regardless of toggle #87

Closed preetsethi closed 5 years ago

preetsethi commented 5 years ago

Hey there!

First and foremost, thanks for all the work with render_async, we use it everyday in production!

I was the guy using the toggle feature prior to your release. After updating to 2.1.1, I noticed some strange behavior: when turbolinks is enabled, the _renderAsyncFunction will fire regardless of the toggle configuration.

I believe this little section of code is responsible

  <% if turbolinks %>
  document.addEventListener("turbolinks:load", function (e) {
    e.target.removeEventListener(e.type, arguments.callee);
    _renderAsyncFunction();
  });
  <% elsif !toggle %>
  document.addEventListener("DOMContentLoaded", _renderAsyncFunction);
  <% end %>

I'm sending you a quick PR, let me know if you want me to change anything.

If this isn't the intended behavior, could you please elaborate on your thinking?

Thanks!

edit:

Realized I can probably clarify and describe our use case a bit more.

Assume the following render_async call:

<%= render_async performance_stats_path(campaign), toggle: { selector: "#load-campaign-#{campaign.id}-stats", event: 'click'}, interval: 60000 do %>
  <a id="<%= "load-campaign-#{campaign.id}-stats" %>" class="btn btn-default btn-xs">
    Load Statistics <i class="fa fa-line-chart ld ld-surprise"></i>
  </a>
<% end %>

Basically we render a button as a placeholder and the button contains the toggle selector. With turbolinks off, everything functions as expected: when you click the button, render_async works its magic and loads the partial, replacing the button with some awesome stats for our customers.

When turbolinks config is enabled, the partial is loaded on turbolinks:load regardless of any toggle configuration. It took me a while to actually root this out because I use all of the following:

  1. rails fragment caching
  2. turbolinks (which has its own caching mechanism)
  3. render_async (with caching, toggle, and interval used in different situations)

and I did not want to file an issue prematurely.

We're running my branch in staging now and everything is functioning as expected. We even have a render_async call with caching, toggle, and interval + turbolinks, and it's working well on our initial evaluation.

Once again, thanks!

nikolalsvk commented 5 years ago

Hey @preetsethi, thanks for submitting the issue! Explaining everything in details really helped, thank you once again!

Sorry for making this bug, I will try to resolve it ASAP and release a new version that fixes it.

nikolalsvk commented 5 years ago

I've just released https://rubygems.org/gems/render_async/versions/2.1.2, let me know if it works for you @preetsethi