Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pink-carpets-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@solid-primitives/pagination": minor
---

createInfiniteScroll now exposes .loading/.error on pages, handles null refs,
118 changes: 79 additions & 39 deletions packages/pagination/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type Accessor,
batch,
type JSX,
type Resource,
type Setter,
createComputed,
createMemo,
Expand Down Expand Up @@ -350,74 +351,113 @@ declare module "solid-js" {
}
}

export type _E = JSX.Element;

/**
* Provides an easy way to implement infinite scrolling.
*
* ```ts
* const [pages, loader, { page, setPage, setPages, end, setEnd }] = createInfiniteScroll(fetcher);
* const [pages, loader, { page, setPage, setPages, end, setEnd }] =
* createInfiniteScroll(fetcher);
* ```
* @param fetcher `(page: number) => Promise<T[]>`
* @return `pages()` is an accessor contains array of contents
* @property `pages.loading` is a boolean indicator for the loading state
* @property `pages.error` contains any error encountered
* @return `infiniteScrollLoader` is a directive used to set the loader element
* @method `page` is an accessor that contains page number
* @method `setPage` allows to manually change the page number
* @method `setPages` allows to manually change the contents of the page
* @method `end` is a boolean indicator for end of the page
* @method `setEnd` allows to manually change the end
* @return `pages` an accessor over the concatenated items array. The accessor
* also carries reactive `loading` and `error` fields forwarded from the
* underlying `solid-js` `Resource`, so their behavior matches any other
* resource in a Solid app.
* @return `loader` ref-callback for your sentinel element (e.g. `<div ref={loader} />`).
* @method `page` current page index accessor.
* @method `setPage` manually change the page.
* @method `setPages` replace the entire concatenated items array.
* @method `end` whether we've reached the end (fetch returned empty).
* @method `setEnd` manually set end.
* @method `refetch` imperatively refetch data.
*/
type Resp<T> = { page: number; items: T[] };
export function createInfiniteScroll<T>(fetcher: (page: number) => Promise<T[]>): [
pages: Accessor<T[]>,
loader: (el: Element) => void,
pages: Accessor<T[]> & Pick<Resource<T[]>, "loading" | "error">,
loader: (el: Element | null) => void,
options: {
page: Accessor<number>;
setPage: Setter<number>;
setPages: Setter<T[]>;
end: Accessor<boolean>;
setEnd: Setter<boolean>;
refetch: (info?: unknown) => Resp<T> | Promise<Resp<T> | undefined> | null | undefined;
},
] {
const [pages, setPages] = createSignal<T[]>([]);
const [items, setItems] = createSignal<T[]>([]);
const [page, setPage] = createSignal(0);
const [end, setEnd] = createSignal(false);

let add: (el: Element) => void = noop;
if (!isServer) {
const io = new IntersectionObserver(e => {
if (e.length > 0 && e[0]!.isIntersecting && !end() && !contents.loading) {
setPage(p => p + 1);
}
});
onCleanup(() => io.disconnect());
add = (el: Element) => {
io.observe(el);
tryOnCleanup(() => io.unobserve(el));
};
}
// Tag each response with the page it came from so we can ignore stale/duplicate appends
const wrapped = async (p: number): Promise<Resp<T>> => ({
page: p,
items: await fetcher(p),
});

const [contents] = createResource(page, fetcher);
const [res, { refetch }] = createResource<Resp<T>, number>(page, wrapped);

let lastAppended = -1;
createComputed(() => {
const content = contents.latest;
if (!content) return;
// Read via `.latest` so a changing source signal doesn't propagate
// suspense to an enclosing <Suspense> boundary on every page change.
const r = res.latest;
if (!r) return;
batch(() => {
if (content.length === 0) setEnd(true);
setPages(p => [...p, ...content]);
if (r.items.length === 0) {
setEnd(true);
return;
}
if (r.page !== lastAppended) {
setItems(prev => [...prev, ...r.items]);
lastAppended = r.page;
}
});
});

let io: IntersectionObserver | null = null;
let observed: Element | null = null;
const loader = (el: Element | null) => {
if (isServer) return;
if (observed && io) {
io.unobserve(observed);
observed = null;
}
if (!io) {
io = new IntersectionObserver(
entries => {
if (!entries.some(e => e.isIntersecting)) return;
if (end() || res.loading) return;
setPage(p => p + 1);
},
{ root: null, rootMargin: "0px 0px 50px 0px", threshold: 0 },
);
onCleanup(() => {
io?.disconnect();
io = null;
});
}
if (el) {
io.observe(el);
observed = el;
}
};
Comment on lines +425 to +443

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🌐 Web query:

Does IntersectionObserver re-invoke its callback when the observed element remains intersecting and no intersection state change occurs?

💡 Result:

No, the IntersectionObserver callback does not re-invoke when an observed element remains intersecting and no intersection state change (crossing a threshold) occurs [1][2][3]. The callback is specifically designed to trigger only when the visibility of the target element crosses one of the observer's defined thresholds [1][2][3]. If the element remains within its current state (i.e., its intersection ratio does not cross any defined threshold), the observer does not fire the callback [1][2][4]. There is one notable exception to this rule: the callback is guaranteed to fire at least once during the first render cycle after the observe method is called, regardless of whether a threshold has been crossed [5]. This initial invocation serves to provide the observer with the target's current intersection status [5]. Following this initial report, subsequent invocations will only occur when a threshold-crossing event takes place [1][2][5].

Citations:


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Map the file structure first
ast-grep outline packages/pagination/src/index.ts --view expanded

# Show the relevant section with line numbers
sed -n '360,520p' packages/pagination/src/index.ts

# Find where `res.loading`, `end()`, and `setPage` are defined/used
rg -n "res\.loading|end\(\)|setPage\(|IntersectionObserver|onCleanup|error" packages/pagination/src/index.ts

Repository: solidjs-community/solid-primitives

Length of output: 4979


Auto-pagination can stall on intersecting sentinels and skip failed pages.

  • If the sentinel stays intersecting while res.loading flips false, no new observer event fires, so short pages can stop loading until the element leaves and re-enters view.
  • On fetch error, the next intersection advances page instead of retrying the failed request, so the failed page is skipped.

Re-check the sentinel when loading/error state changes, or gate setPage on a successful fetch state.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/pagination/src/index.ts` around lines 425 - 443, The auto-pagination
logic in the IntersectionObserver callback can stall because it only advances
when a new intersection event fires, and it can also skip a failed page by
incrementing page even after an error. Update the observer handling around the
IntersectionObserver setup, setPage, end(), and res.loading so the sentinel is
re-checked whenever loading or error state changes, and only advance to the next
page when the previous fetch succeeded. If the current request failed, retry the
same page instead of incrementing.


const pages = (() => items()) as Accessor<T[]> & Pick<Resource<T[]>, "loading" | "error">;
Object.defineProperties(pages, {
loading: { get: () => res.loading, enumerable: true },
error: { get: () => res.error, enumerable: true },
});

return [
pages,
add,
loader,
{
page: page,
setPage: setPage,
setPages: setPages,
end: end,
setEnd: setEnd,
page,
setPage,
setPages: setItems,
end,
setEnd,
refetch,
},
];
}