Persisting elements across page loads

suggest change

Consider a social site that uses Turbolinks and shows a friend count on every page. You have 3 friends. JavaScript then updates that count to 4 after a new friend is added. You click the browser Back button, and the counter shows 3 again.

Turbolinks restores the previous page from its snapshot, so the cached DOM is not read from the database again.

Render the count in a <div> with an id of friend-count:

<div id="friend-count" data-turbolinks-permanent>3 friends</div>

data-turbolinks-permanent tells Turbolinks to keep that element. It copies the element you have now onto the next page, including a restored snapshot. It does not reload the count from the database. The official docs say:

Designate permanent elements by giving them an HTML id and annotating them with data-turbolinks-permanent. Before each render, Turbolinks matches all permanent elements by id and transfers them from the original page to the new page, preserving their data and event listeners.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents