liaisev5.4.0
/Pagination

Guide

Pagination

This page shows how to read a list endpoint that returns one page at a time, whether your API pages by cursor, offset or page number. liaise has two tools for it:

  • paginate walks the pages, asking for each one only when you ask for the next. Use it for a “Load more” button, an export or a sync job.
  • createPager holds the page a screen shows, and moves it: to page 7, to the next or previous page, or to page 1 of a new filter. It keeps a state your UI subscribes to, and a late reply never overwrites a newer page. Use it for a numbered pager, Previous and Next over cursors, or infinite scroll (A numbered pager).
import { createApi, defineRequest, paginate } from 'liaise'

type Item = { id: string; name: string }
type Page = { items: Item[]; cursor?: string }

const listItems = defineRequest<Page, { limit: number; cursor?: string }>()({
  method: 'GET',
  path: '/items',
})

const api = createApi({ baseUrl: 'https://api.example.com', requests: { listItems } })

const pages = paginate(api.listItems, { limit: 50 }, {
  next: (p, prev) => (p.data.cursor ? { ...prev, cursor: p.data.cursor } : undefined),
})
// Nothing has been fetched yet.

async function loadMore() {
  const { value: page, done } = await pages.next() // one request per click
  if (done) return hideLoadMore()
  if (page.error) return showError(page.error)
  render(page.data.items)
}

render, showError and hideLoadMore stand for your own code, and your “Load more” button calls loadMore. Creating pages sends nothing, and each click sends one request: /items?limit=50, then ?limit=50&cursor=… with the cursor from the page before, until a page comes back without one and the next click finds done.

Walking every page

For an export or a sync job, where you want every page, loop over the pages with for await:

import { createApi, defineRequest, paginate } from 'liaise'

type Item = { id: string; name: string }
type Page = { items: Item[]; cursor?: string }

const listItems = defineRequest<Page, { limit: number; cursor?: string }>()({
  method: 'GET',
  path: '/items',
})

const api = createApi({ baseUrl: 'https://api.example.com', requests: { listItems } })

for await (const page of paginate(api.listItems, { limit: 50 }, {
  next: (p, prev) => (p.data.cursor ? { ...prev, cursor: p.data.cursor } : undefined),
})) {
  if (page.error) break
  render(page.data.items)
}

The loop asks for /items?limit=50, then for ?limit=50&cursor=… with each page’s cursor, and stops after the first page that comes back without one. break stops it sooner, and no further page is requested.

Getting to the next page

  • next returns the params for the next page. It gets the page just loaded and the params that loaded it, so the usual case is a spread. liaise never has to guess whether your API calls it cursor, page_token or after, and the same shape covers every scheme:

    // offset
    next: (p, prev) => p.data.items.length === prev.limit
      ? { ...prev, offset: prev.offset + prev.limit }
      : undefined
    
    // page number, driven by a Link header
    next: (p, prev) => p.response.headers.get('link')?.includes('rel="next"')
      ? { ...prev, page: prev.page + 1 }
      : undefined
  • Return undefined or null to stop. A for await loop ends there, and the next pages.next() gives done without sending a request.

When to stop

  • An error page ends it. You get the error page, and then a loop ends, or the next pages.next() gives done, because there is no data to read the next cursor from. You see what failed. It never stops quietly.

  • maxPages has no default. Set it if you want a ceiling, as in paginate(api.listItems, { limit: 50 }, { next, maxPages: 100 }). liaise doesn’t pick a number, because a silent cut-off at an arbitrary page looks exactly like reaching the last one.

  • truncated tells a capped walk from a finished one. Once the loop has ended, pages.truncated is true when maxPages stopped it and your next says there was another page, and false when the pages ran out, an error page ended it, or the cap landed exactly on the last page. Reading it sends no request, but it calls your next once with the last page, on the first read, and remembers the answer.

    import { createApi, defineRequest, paginate } from 'liaise'
    
    type Row = { id: string }
    type Page = { items: Row[]; cursor?: string }
    
    const listRows = defineRequest<Page, { limit: number; cursor?: string }>()({
      method: 'GET',
      path: '/rows',
    })
    
    const api = createApi({ baseUrl: 'https://api.example.com', requests: { listRows } })
    
    const pages = paginate(api.listRows, { limit: 1000 }, {
      next: (p, prev) => (p.data.cursor ? { ...prev, cursor: p.data.cursor } : undefined),
      maxPages: 2,
    })
    
    for await (const page of pages) {
      if (page.error) break
      save(page.data.items)
    }
    if (pages.truncated) warn('Export cut at 2,000 rows')

    The loop sends /rows?limit=1000 and then ?limit=1000&cursor=c2, and stops at the cap. A third page exists, so the export warns instead of ending quietly. truncated is false while the walk is still going, so read it after the loop.

  • A page you don’t ask for is never requested. Breaking out of a for await loop sends no further request, and neither does a pages you stop calling next() on.

Options and pages

  • Every other option applies to every page. Any of the CallOptions, such as signal, timeout or headers, goes with each request, so one signal cancels every page, whether you walk them or load them one at a time.
  • paginate yields pages. Read the items from each page yourself. Flattening them would mean guessing which field holds the array.

Going straight to a page

paginate only moves forward, from each page to the one after it. A screen that shows one page at a time needs more: “go to page 7”, a Previous button, a page number or a cursor kept in the address, and a filter that starts again at page 1. That is createPager. It takes the same endpoints as paginate, and you tell it how your API pages in one of two ways:

  • toParams makes a numbered pager, for APIs that page by offset or page number, where any page can be opened directly.
  • next makes a cursor pager, for APIs where each page names the one after it. It is the same next that paginate takes.

Add items to either one and it becomes one growing list, for infinite scroll. Every option is listed in createPager() options and state.

A numbered pager

import { createApi, defineRequest, createPager } from 'liaise'

type Order = { id: string; total: number }
type Page = { items: Order[]; totalCount: number }
type Params = { offset: number; limit: number; q?: string }

const listOrders = defineRequest<Page, Params>()({ method: 'GET', path: '/orders' })
const api = createApi({ baseUrl: 'https://api.example.com', requests: { listOrders } })

const limit = 25
// Typed as Params, so setParams can add the filter the first request doesn't send.
const unfiltered: Params = { offset: 0, limit }

const pager = createPager(api.listOrders, unfiltered, {
  toParams: (page, params) => ({ ...params, offset: (page - 1) * limit }),
  pageCount: r => Math.ceil(r.data.totalCount / limit),
  // The page in the address, so a reload or a shared link opens the same page.
  start: Number(new URLSearchParams(location.search).get('page') ?? 1),
})

pager.subscribe(() => {
  const { data, page, pageCount, loading, error } = pager.state
  render(data?.items ?? [], { page, pageCount, loading, error })
  if (page !== null) history.replaceState(null, '', `?page=${page}`)
})

pager.load()
// Your page buttons call pager.goTo(n), and Previous and Next call pager.prev() and pager.next().

function onFilter(q: string) {
  pager.setParams({ offset: 0, limit, q }) // back to page 1, with the new filter
}

render stands for your own code. Creating the pager sends nothing, and pager.load() opens the page in the address: with ?page=2, it sends /orders?offset=25&limit=25. After every change, the pager calls your listener, which renders pager.state and writes the page back to the address.

  • toParams builds each page’s params from the page number, counted from 1, and the base params: the ones you passed to createPager, which setParams replaces. liaise doesn’t guess whether your API counts by offset or by page. Give the base params a type with every field setParams may add, as the example does with Params. Otherwise TypeScript reads their type from the object you pass, and setParams with a q doesn’t compile.
  • pageCount reads how many pages there are from a result: 230 orders at 25 a page make 10. hasNext is false on the last page. Without pageCount, state.pageCount is null and hasNext stays true, because the pager can’t know where the list ends.
  • The shown page stays while the next one loads. goTo(3) sets loading and target: 3, and data and page are still page 2’s until page 3 arrives. A failed move leaves the shown page as well, with error set until the next success.
  • A page that doesn’t exist sends nothing. goTo(0), goTo(2.5) and goTo(11) of 10 pages resolve with null, and so do prev() on page 1 and next() on the last page.
  • Two quick clicks on Next move two pages. next() counts from the page that is loading, so two clicks from page 3 land on page 5.
  • Pages you’ve seen come back without a request. Going back to page 2 shows it from the cache, at once. refresh() asks for the shown page again and replaces its cache entry, setParams forgets every page, and cache: false asks on every visit.
  • A filter change is setParams. It opens page 1 with the new base params, so onFilter('acme') sends /orders?offset=0&limit=25&q=acme. A page of the old filter that was still loading never shows.

Previous and Next over cursors

import { createApi, defineRequest, createPager } from 'liaise'

type Item = { id: string; name: string }
type Page = { items: Item[]; next: string | null }
type Params = { limit: number; cursor?: string }

const listItems = defineRequest<Page, Params>()({ method: 'GET', path: '/items' })
const api = createApi({ baseUrl: 'https://api.example.com', requests: { listItems } })

// A cursor in the address opens that page, after a reload or from a shared link.
const cursor = new URLSearchParams(location.search).get('cursor')

const pager = createPager(api.listItems, { limit: 50 }, {
  next: (r, params) => (r.data.next ? { ...params, cursor: r.data.next } : undefined),
  from: cursor ? { limit: 50, cursor } : undefined,
})

pager.subscribe(() => {
  const { data, params, page, hasPrev, hasNext, loading } = pager.state
  render(data?.items ?? [], { page, hasPrev, hasNext, loading })
  if (params) history.replaceState(null, '', params.cursor ? `?cursor=${params.cursor}` : location.pathname)
})

pager.load()
// Your Previous and Next buttons call pager.prev() and pager.next().

function firstPage() {
  pager.setParams({ limit: 50 }) // the first page, wherever the pager opened
}

Without a cursor in the address, load() opens the first page, /items?limit=50, as page 1. A reader who reloads on a later page, or follows a shared link, has its cursor in the address, and from opens that page instead: /items?limit=50&cursor=c2. Next asks for ?limit=50&cursor=c3, and the listener writes ?cursor=c3 to the address.

  • Previous goes back through the pages already seen, from the cache, without a request. A cursor API can only be walked forward, so goTo(n) works only for a page this pager has shown and knows the number of, and resolves with null for one it hasn’t reached.
  • A page opened with from has no number. state.page is null there, and on the pages Next reaches from it, because the pager can’t know how many pages came before. hasPrev is false on it, unless your API returns a previous cursor: give prev, which reads the params of the page before from a result, and Previous asks for that page.
  • setParams starts over at the first page, which is page 1 again. In the example, firstPage() sends /items?limit=50, and Next from there is page 2. from isn’t used again.
  • When a page you’ve seen names a different next page, after refresh() or a visit with cache: false, the list has changed. The pages after it are forgotten, and Next follows the new cursor. A next that returns undefined or null makes the shown page the last one.

Infinite scroll

import { createApi, defineRequest, createPager } from 'liaise'

type Post = { id: string; title: string }
type Page = { items: Post[]; next: string | null }

const listPosts = defineRequest<Page, { limit: number; cursor?: string }>()({ method: 'GET', path: '/posts' })
const api = createApi({ baseUrl: 'https://api.example.com', requests: { listPosts } })

function openFeed() {
  const feed = createPager(api.listPosts, { limit: 20 }, {
    next: (r, params) => (r.data.next ? { ...params, cursor: r.data.next } : undefined),
    items: r => r.data.items,
    // As many pages as the reader had before a reload.
    restore: Number(sessionStorage.getItem('feed:pages')) || 1,
  })
  feed.subscribe(() => {
    const { items, loaded, loading, hasNext } = feed.state
    render(items, { loading, hasNext })
    if (!loading) sessionStorage.setItem('feed:pages', String(loaded))
  })
  feed.load()
  return feed
}

const feed = openFeed()
// Your scroll handler calls feed.next() when the reader nears the end of the list.

With items, the pager keeps one list: load() fetches the first page, and each next() appends the following page’s items to state.items. items reads the items from a result, because liaise never guesses which field holds them.

  • state.loaded counts the pages in the list. Save it, as the example does in sessionStorage, and pass it back as restore after a reload. With restore: 3, load() asks for pages 1, 2 and 3 again, one after another, and the list grows as each one lands. loading stays true until the last of them arrives, which is why the example saves the count only when loading is false.
  • The list ends at a page with no items, at the last page pageCount reports, or at a page your next returns nothing for. hasNext is then false, and next() resolves with null without sending anything.
  • A second next() while one is loading returns the same promise, so a scroll handler that fires twice asks for one page.
  • A failure during next() or a restore stops it there. error is set, the pages that arrived stay, loaded counts them, and hasNext stays true, so the next next() asks for the failed page again.
  • refresh() reloads pages 1 to loaded, and the new items replace the shown ones when the last page arrives. A failed refresh keeps the list and sets error: items, loaded and hasNext stay as they were. setParams empties the list and loads page 1 with the new params.

A list always starts at page 1: goTo and prev resolve with null, and start, from and cache have no effect.

With GraphQL

A pager takes a createGraphQL operation as it takes a REST endpoint. Its params are the operation’s variables:

import { createGraphQL, Operation, gql, createPager } from 'liaise'

type Order = { id: string; total: number }
type Vars = { offset: number; limit: number }

const listOrders = new Operation<Vars, { orders: { totalCount: number; nodes: Order[] } }>({
  operation: gql`
    query ListOrders($offset: Int!, $limit: Int!) {
      orders(offset: $offset, limit: $limit) {
        totalCount
        nodes { id total }
      }
    }
  `,
})

const graphql = createGraphQL({ endpoint: 'https://api.example.com/graphql', operations: { listOrders } })

const pager = createPager(graphql.listOrders, { offset: 0, limit: 25 }, {
  toParams: (page, vars) => ({ ...vars, offset: (page - 1) * vars.limit }),
  pageCount: r => Math.ceil(r.data.orders.totalCount / 25),
})

await pager.load()
await pager.goTo(4)

load() sends the operation with { offset: 0, limit: 25 }, and goTo(4) sends it with { offset: 75, limit: 25 }. pageCount reads totalCount from the response, so the state shows page 4 of 10, and going back to page 1 comes from the cache.

With React, Vue and Svelte

pager.state is a frozen snapshot that stays the same object until something changes, and subscribe returns the function that unsubscribes. That is the shape each framework’s own store wrapper expects, so the adapter is a few lines.

React, with useSyncExternalStore:

function usePager<P, R, T>(pager: Pager<P, R, T>): PagerState<P, R, T> {
  return useSyncExternalStore(pager.subscribe, () => pager.state)
}

Vue, with a shallowRef, unsubscribing when the component’s scope ends:

function usePagerState<P, R, T>(pager: Pager<P, R, T>): ShallowRef<PagerState<P, R, T>> {
  const state = shallowRef(pager.state)
  onScopeDispose(pager.subscribe(() => { state.value = pager.state }))
  return state
}

Svelte, as a readable store. It reads the state when its first subscriber arrives, so a move made before then still shows:

function pagerStore<P, R, T>(pager: Pager<P, R, T>): Readable<PagerState<P, R, T>> {
  return readable(pager.state, set => {
    set(pager.state) // a move made before the first subscriber
    return pager.subscribe(() => set(pager.state))
  })
}

Create the pager outside the component, or once per component instance, and call load() when it should start fetching. Creating a pager sends nothing, so it is safe during server rendering.

When a reply arrives late

The latest move wins, per pager. Each move cancels the request this pager started for the move before it, through that request’s own signal. A reply that arrives anyway, from an endpoint or a middleware that ignored its signal, never changes the state. In the numbered example, clicking 3, then 5, then 4 renders only page 4.

  • A move that was overtaken resolves with kind: 'abort', whose body is a DOMException with the message Superseded by a newer move. It never changes state, not even error.
  • This isn’t dedupe. The pager cancels only what it started, so two pagers over the same endpoint, a paginate walk and a plain call never cancel each other.
  • Don’t give a pager an endpoint with dedupe: true when its other calls must survive. dedupe cancels per endpoint, so a pager move cancels the endpoint’s other call in flight (Drop stale calls with dedupe).
  • share: true works. A pager’s request is an ordinary call, so it can join an identical call in flight. When the pager’s move is overtaken, only the pager leaves the shared request, and the other caller still gets its result.
  • Your signal stops the pager. Pass one in the options to stop it, for example when the reader leaves the page. The request in flight is cancelled, loading ends, and error and the shown page stay as they were. That move, and every move after it, resolves with kind: 'abort', whose body is the signal’s reason, and sends nothing more. The other CallOptions, such as headers or timeout, go with every request.

Next: Polling asks an endpoint again on an interval, to keep data fresh or to wait until something is done.

esc