title: How can I prevent duplicate wrappers on a jQuery DataTable when navigating back in the presence of Turbolinks?
body: <p>I'm using jQuery DataTables in a new Rails 4.2 project. I've got a large number of results that need to be filtered on group, project, and status. Choosing the group filters the project selection, via a roundtrip through the application, and the project selection filters the table the same way. I save the group &amp; project in session variables so that, when the user changes pages, then clicks the main link to come back, it will remember their selection, and get them back to where they were. This all works like I intend it to.</p>
  <p>The problem is that using the browser's <em><strong>back button</strong></em> to get back to the table from a different page will result in a duplicate wrapper (with search box, number-of-items dropdown, and pagination links) around the DataTable.</p>
  <p><a href="https://i.stack.imgur.com/c6P6o.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/c6P6o.png" alt="enter image description here" /></a></p>
  <p>I've added the jquery-turbolinks gem.</p>
  <p>I've tried <a href="https://datatables.net/forums/discussion/36875/duplicate-wrapper-with-browser-forward-back-button" rel="nofollow noreferrer">fiddling with event listeners</a>, but I can't make either of those ideas work.</p>
  <p>I've tried making the body of the page exempted from Turbolinks with <code>data-turbolinks=false</code>.</p>
  <p>I've tried changing the event my script(s) key on to something other than <code>$(document).ready()</code>, like <code>page:load</code>. None of them prevent this.</p>
  <p>I've tried loading all the scripts for the DataTable as exempted from Turbolinks with <code>data-turbolinks-eval=false</code> in that specific page (with <code>content_for :head</code>).</p>
  <p>The only thing I've found that will prevent this wrapper duplication from happening is to simply remove the call to include the turbolinks JS from the assets entirely. And I'd have done this already if I didn't know that smarter people than me think it ought to be a part of the application. I don't know what to do. I'm looking for the elegant way to handle this.</p>
  <p>UPDATE: Four years later, I'm using Rails 6.1, and still using Ag-Grid extensively. I'd like to try the new hotness from Rails <a href="https://github.com/hotwired/hotwire-rails" rel="nofollow noreferrer">Hotwire</a>, but it seems to be built on Turbolinks. So I've taken another pass at getting Ag-Grid to play nicely with Turbolinks, and it still has all the same problems as DataTables, and I STILL can't figure out a way forward with it.</p>
  <p>I got some small progress with a tip from <a href="https://datatables.net/forums/discussion/36875/duplicate-wrapper-with-browser-forward-back-button#Comment_136960" rel="nofollow noreferrer">this page on DataTables</a>, by putting <code>data-turbolinks=&quot;false&quot;</code> in the div for the table (not the page, like I had previously tried). For some navigation, this &quot;worked.&quot; By &quot;worked,&quot; I mean that it prevented duplicating the table, but it still reloads the whole table.</p>
  <p>I also found this <a href="https://github.com/turbolinks/turbolinks/issues/87" rel="nofollow noreferrer">fascinating discussion</a> about how Basecamp apparently deals with the cache in the presence of Turbolinks, but it didn't help for Ag-Grid.</p>
  <p>I've discovered that the right incantation to prevent duplication of table elements is to use <code>document.addEventListener('DOMContentLoaded', function() {})</code> as my key. It seems to respect all the different cases of using links or back/forward buttons for navigation, without duplicating. However, I'm losing any rows in the table past what is showing on the screen, and the whole table needs to have data fetched and displayed again, anyway, so that really doesn't solve anything.</p>
  <p>That's the part that continues to fascinate and frustrate me about this situation. If I DON'T use Tubolinks, these JS tables work GREAT with the browser navigation buttons. I can page back and forward, and none of the data has to get reloaded. If I DO use Turbolinks, and manage to prevent the duplication of the table elements, I need to reload the table data. I'm clearly getting caught in the middle of 2 different JS caches, and after 4 years, I still can't figure out how to get them to play nicely together.</p>