diff --git a/assets/js/main.js b/assets/js/main.js index b9092ffc3..8c8ea6247 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -48,6 +48,7 @@ var debug = 0 ? console.log.bind(console, '[index]') : function () {}; { let containerScrollTops = {}; + let turboLoaded = false; // To preserve scroll position in scrolling elements on navigation add data-turbo-preserve-scroll-container="somename" to the scrolling container. addEventListener('turbo:click', () => { @@ -56,14 +57,15 @@ var debug = 0 ? console.log.bind(console, '[index]') : function () {}; }); }); - addEventListener('turbo:render', () => { - if (document.documentElement.hasAttribute('data-turbo-preview')) { + addEventListener('turbo:load', () => { + if (turboLoaded) { // Minimal Analytics does not support SPA out of the box, so we need to manually track page views on Turbo navigation. - // Render may be called twice, once for preview and once for the real render. - // Track only on the real render. window.track('page_view'); } + turboLoaded = true; + }); + addEventListener('turbo:render', () => { document.querySelectorAll('[data-turbo-preserve-scroll-container]').forEach((ele) => { const containerScrollTop = containerScrollTops[ele.dataset.turboPreserveScrollContainer]; if (containerScrollTop) {