Pagination
URL-driven page state via searchParams. The loader slices the dataset by ?page=N; links navigate the browser, no client JS. Refresh, share, and back/forward all work because the page number lives in the URL.
Tip
Every ?page=N URL is bookmarkable and crawlable — unlike client-side state. Combine server pagination with infinite scroll by pre-rendering page 1 then fetching subsequent pages via the same URL.
Rows 11–20 of 47
- #11 · Item 11
- #12 · Item 12
- #13 · Item 13
- #14 · Item 14
- #15 · Item 15
- #16 · Item 16
- #17 · Item 17
- #18 · Item 18
- #19 · Item 19
- #20 · Item 20
// Page reads ?page from searchParams; loader returns the slice +
// pagination metadata. Links carry ?page=N — no client JS, no
// state. Browser back/forward Just Works.
export async function loader({ request }) {
const url = new URL(request.url);
const page = Math.max(1, parseInt(url.searchParams.get("page") ?? "1", 10));
const start = (page - 1) * PAGE_SIZE;
return {
rows: items.slice(start, start + PAGE_SIZE),
page,
totalPages: Math.ceil(items.length / PAGE_SIZE),
};
}
// Render: <a href={"?page=" + (page - 1)}>← prev</a>