Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
preetsethi opened this issue Aug 29, 2019 · 2 comments · Fixed by #88

Comments

@preetsethi
Copy link
Contributor

preetsethi commented Aug 29, 2019

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
Copy link
Owner

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants