You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Running Ruby on a installation with 400+ posts, and get duplicates in the infinite scroll.
I have an update to main.js which resolves it.
Ruby's main.js looks like this in the repo, I'm a little lost on how to create a branch here..
Here is my main.js which uses a Set() to track post ID and prevent duplicates, feel free to merge it.
(()=>{// Track loaded post IDs to prevent duplicatesconstloadedPosts=newSet();// Initialize paginationpagination(true);// Handle mobile menuconstburger=document.querySelector('.gh-burger');if(burger){burger.addEventListener('click',function(){if(document.body.classList.contains('is-head-open')){document.body.classList.remove('is-head-open');}else{document.body.classList.add('is-head-open');}});}// Initialize lightbox for imageslightbox('.kg-image-card > .kg-image[width][height], .kg-gallery-image > img');// Handle responsive iframesreframe(document.querySelectorAll(['.gh-content iframe[src*="youtube.com"]','.gh-content iframe[src*="youtube-nocookie.com"]','.gh-content iframe[src*="player.vimeo.com"]','.gh-content iframe[src*="kickstarter.com"][src*="video.html"]','.gh-content object','.gh-content embed'].join(',')));// Initialize dropdowndropdown();/** * Enhanced pagination function that handles both infinite scroll and load-more button functionality * * @param {boolean} isInitial - If true, sets up infinite scroll using IntersectionObserver. * If false, uses load-more button functionality. * @param {Function} callback - Optional callback function executed after loading new posts. * Receives (uniquePosts, nextElement) as parameters. * @param {boolean} waitForImages - If true, new posts are initially hidden until images load. * Helps prevent layout shifts during image loading. */functionpagination(isInitial,callback,waitForImages=false){// Main container for postsconstcontainer=document.querySelector('.gh-feed');if(!container)return;// State tracking for preventing concurrent loadsletisLoading=false;// Element used to trigger infinite scroll// Falls back through multiple options to find a suitable trigger elementletnextElement=container.nextElementSibling||container.parentElement.nextElementSibling||document.querySelector('.gh-foot');// Load more button for manual pagination if infinite scroll is disabledconstloadMoreButton=document.querySelector('.gh-loadmore');// Early cleanup: Remove load more button if there are no more pages to loadif(!document.querySelector('link[rel=next]')){loadMoreButton?.remove();return;}/** * Fetches and appends posts from the next page * - Fetches HTML from next page URL * - Parses response and extracts posts * - Filters out duplicate posts using URL tracking * - Appends unique posts to container * - Updates pagination links * - Handles visibility for image loading */asyncfunctionloadNextPage(){constnextPageUrl=document.querySelector('link[rel=next]');if(!nextPageUrl)return;try{constresponse=awaitfetch(nextPageUrl.href);consthtml=awaitresponse.text();constparser=newDOMParser();constdoc=parser.parseFromString(html,'text/html');// Extract all posts from next page, excluding featured and related postsconstnewPosts=Array.from(doc.querySelectorAll('.gh-feed:not(.gh-featured):not(.gh-related) > *'));// Remove any posts that have already been loaded to prevent duplicates// Uses post URLs stored in loadedPosts Set for trackingconstuniquePosts=newPosts.filter(post=>{constpostLink=post.querySelector('a.post-link');if(!postLink)returnfalse;constpostUrl=postLink.href;if(loadedPosts.has(postUrl)){returnfalse;}loadedPosts.add(postUrl);returntrue;});// Use DocumentFragment for better performance when adding multiple posts// Optionally hide posts initially if waiting for images to loadconstfragment=document.createDocumentFragment();uniquePosts.forEach(post=>{constimportedPost=document.importNode(post,true);if(waitForImages){importedPost.style.visibility='hidden';}fragment.appendChild(importedPost);});container.appendChild(fragment);// Update or remove pagination links based on next page availability// Removes load more button when reaching the last pageconstnewNextLink=doc.querySelector('link[rel=next]');if(newNextLink&&newNextLink.href){nextPageUrl.href=newNextLink.href;}else{nextPageUrl.remove();loadMoreButton?.remove();}// Execute callback with newly loaded posts and next element referenceif(callback){callback(uniquePosts,nextElement);}}catch(error){console.error('Error loading next page:',error);nextPageUrl.remove();loadMoreButton?.remove();}}/** * Handles infinite scroll functionality * - Checks if more pages are available * - Prevents concurrent loading * - Triggers load when next element comes into view */asyncfunctionhandleScroll(){if(!document.querySelector('link[rel=next]'))return;if(isLoading)return;if(nextElement.getBoundingClientRect().top<=window.innerHeight){isLoading=true;awaitloadNextPage();isLoading=false;}}/** * Initialize pagination based on mode (infinite scroll vs load more button) * For infinite scroll (isInitial = true): * - Sets up IntersectionObserver to detect when more content should load * - Handles both immediate and delayed loading based on waitForImages * For load more button (isInitial = false): * - Attaches click handler to load more button */if(isInitial){constobserver=newIntersectionObserver(asyncfunction(entries){if(isLoading)return;if(entries[0].isIntersecting){isLoading=true;if(waitForImages){awaitloadNextPage();}else{while(nextElement.getBoundingClientRect().top<=window.innerHeight&&document.querySelector('link[rel=next]')){awaitloadNextPage();}}isLoading=false;}});observer.observe(nextElement);}elseif(loadMoreButton){loadMoreButton.addEventListener('click',loadNextPage);}}})();
The text was updated successfully, but these errors were encountered:
Running Ruby on a installation with 400+ posts, and get duplicates in the infinite scroll.
I have an update to main.js which resolves it.
Ruby's main.js looks like this in the repo, I'm a little lost on how to create a branch here..
Here is my main.js which uses a Set() to track post ID and prevent duplicates, feel free to merge it.
The text was updated successfully, but these errors were encountered: