From 46af09e348b688341a2b08a35448df536cbedfac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 21 Aug 2020 15:14:57 +0200 Subject: [PATCH] Destroy Sticky elements before leaving the page When Foundation initializes a page that has any sticky element, it loads a window scroll listener into window object to handle the sticky element positioning when scrolling. This works great until user moves to a page without sticky elements and the window listeners remain attached to the window object on this new page too when there is no need to run the scrollListener and there is no sticky elements so the window scroll listener cannot find the sticky object on that page a lot of errors are thrown when user scrolls. With this approach we are destroying sticky elements before leaving the page which also remove the Sticky scrollListener from the window object. I do not know how to write a test to demonstrate this fix, but at least there is some steps to reproduce this behavior: 0. Undo this commit 1. Go to any proposal page 2. Click on any link (with Turbolinks enabled) 3. Scroll the page 4. Check the console log. Yo will find as error occurrences as pixels you scrolled. --- app/assets/javascripts/foundation_extras.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/assets/javascripts/foundation_extras.js b/app/assets/javascripts/foundation_extras.js index de709505d..435bd7e49 100644 --- a/app/assets/javascripts/foundation_extras.js +++ b/app/assets/javascripts/foundation_extras.js @@ -3,6 +3,13 @@ App.FoundationExtras = { initialize: function() { $(document).foundation(); + }, + destroy: function() { + if ($(".sticky").length > 0) { + $(".sticky").foundation("_destroy"); + } } }; + + $(document).on("turbolinks:before-visit", App.FoundationExtras.destroy); }).call(this);