Hi @yoda71 ,
I am from Mageplaza - Shopify solution expert.
Your script builds the next page URL like this:
const t = `{{ shop.url }}/${this.dataset.next}`;
const i = new URL(t);
i.searchParams.set("page", e);
But this is always resetting to the same dataset.next (probably collection?page=1), and simply changing the page parameter manually.
This logic ignores the fact that Shopify’s pagination can change structure, especially with filters, sorting, tags, or app proxies (like ?filter.v.availability=1&page=2, or ?sort_by=manual).
So dataset.next becomes stale after the first load and you’re stuck modifying ?page=2, ?page=3 manually based on the original URL — not the updated one Shopify would normally generate.
Fix: Dynamically update the data-next value after each fetch
To fix it, you should extract the new data-next from the response HTML after each scroll.
Here’s a modified fetchNextPage() that correctly updates the data-next attribute dynamically:
Updated fetchNextPage() Function
Replace your current fetchNextPage() with:
fetchNextPage(e) {
if (e <= this.dataset.pageSize) {
const basePath = this.dataset.next?.split("?")[0]; // safely get the path before query
const url = new URL(`${window.location.origin}/${basePath}`);
url.searchParams.set("page", e);
fetch(url.href, { method: "GET", headers: { "Content-Type": "text/html" } })
.then((res) => res.text())
.then((html) => {
const doc = new DOMParser().parseFromString(html, "text/html");
// Append new products
const newGrid = doc.querySelector(this.gridName)?.innerHTML;
if (newGrid) {
this.gridContainer.insertAdjacentHTML("beforeend", newGrid);
}
// Update dataset.next if the next
**Ensure Liquid Snippet is Properly Set**
Your infinite-scroll.liquid snippet should look like this inside main-collection-product-grid.liquid:
```markup
Use remove: request.host to avoid duplicating host names when rebuilding URLs in JS.
Optional Fix: Fallback for Shopify themes that no longer support ?page=2
If Shopify removes support for classic ?page=2 navigation, you’ll need to dynamically follow the real next link using something like this:
const newPaginateLink = doc.querySelector('.pagination .next a')?.getAttribute('href');
if (newPaginateLink) {
this.dataset.next = newPaginateLink.replace(window.location.origin, "");
}
Please let me know if it works as expected!
Best regards!