Skip to content
Flow-UILive
On this page

Pagination and pull to refresh

PageStore can paginate and refresh. The backend still describes the widgets. The host supplies the next cursor through PageLoader.

Ayush MishraPublished 6 min read

PageStore can paginate and refresh. The backend still describes the widgets. The host supplies the next cursor through PageLoader. Flow-UI does not invent page two. It echoes pagination.postback as opaque JSON on PageRequest.Kind.nextPage and appends the sections that come back. Pull to refresh is a backend flag on the envelope, RefreshModel.pullToRefresh, mapped from pull_to_refresh.

The envelope fields are in Page envelope. The loader kinds are in PageLoader: the networking seam. Caching those bytes is host policy, from caching server driven payloads. This article is what PageStore and FlowPageView actually do with has_more, postback, and the refresh gesture.

Pagination is a sentinel plus an opaque postback#

PaginationModel has hasMore and optional postback. When has_more is true, FlowPageView puts a ProgressView sentinel after the last section. The sentinel’s .task calls store.loadNextPage().

loadNextPage returns immediately unless the page is .loaded, hasMore is true, and isLoadingMore is false. It sets isLoadingMore, builds PageRequest(pageID:kind: .nextPage(postback: current.pagination?.postback)), and fetches. On success it appendNextPage. On failure it swallows the error. A failed pagination fetch does not blank a healthy page. The sentinel stays. The next appearance can retry.

PageStore.swiftswift
public func loadNextPage() async {
    guard case .loaded(let current) = state,
          current.pagination?.hasMore == true,
          !isLoadingMore else { return }
    isLoadingMore = true
    defer { isLoadingMore = false }
    let request = PageRequest(pageID: pageID, kind: .nextPage(postback: current.pagination?.postback))
    // ...
}

postback can be { "cursor": "abc" }, a string, a number, or any JSON the backend likes. The client must not require a specific shape. Echo it. That is the whole cursor API.

swift
case nextPage(postback: JSONValue?)

The next document is still a page envelope. appendNextPage appends next.sections onto the current page and replaces pagination with the next payload’s pagination (so has_more can become false). It does not replace nav, header, or footer from page two. If you needed those to change, that is a refresh or a replace_page mutation, not pagination.

Merge identity#

Two responses that were each internally consistent can still collide once merged. appendNextPage calls makeWidgetIdentifiersUnique(recordingTo: diagnostics) after the append. Duplicate ids pick up #2 suffixes. Send real ids from the backend for rows that persist across pages. Stable widget identities is the fallback format.

appendNextPage re-reads state after the await. An api replace_widget that landed while the next page was in flight is kept. If a refresh or replace_page changed pagination.postback while the fetch was in flight, the response is dropped because the cursor no longer describes what is on the page.

Pull to refresh keeps content while in flight#

RefreshModel is { "pull_to_refresh": true }. Default when you construct in Swift is true. When the key is omitted from JSON, refresh on the page is nil, and FlowPageView treats page.refresh?.pullToRefresh ?? false, so the gesture is off unless the backend sent the object (or you built a PageModel in tests with the Swift default). If you want the gesture, send "refresh": { "pull_to_refresh": true }.

The scroll view uses .refreshable when that flag is true. The action is await store.refresh().

refresh() comments in source: keep current content visible while the refresh is in flight. Implementation: stateStore.reset(), then fetch(.refresh). It does not set .loading. Users still see widgets. Stepper counts and accordion expansion clear because the store reset. That is intended: a new document should not inherit leftover UI flags.

fetch still cancels an older fetch, decodes a PageResponse, and sets .loaded or .empty. On throw (other than cancellation) it sets .failed. A failed pagination does not blank the page. A failed refresh can. Those policies are different on purpose. Retry from the failed state calls loadInitial().

refresh_page actions call the same refresh(). Pull and action share the loader kind .refresh so your PageLoader can skip cache or send Cache-Control: no-cache for that kind. See caching server driven payloads.

Widget state versus page content#

Pagination appends leave WidgetStateStore untouched. A stepper still in the list keeps its count while new sections appear. Refresh clears the store. If that distinction is wrong for your product, you are fighting the store. Do not copy counts into JSON on the client to survive refresh; let the backend send initial again.

Backend rules#

  1. Set has_more false on the last page or the sentinel loops forever.
  2. Treat postback as your cursor. Change it when the next slice changes. Keep it stable for retries of the same slice.
  3. Return sections to append, not a full replay of page one, unless you like duplicate rows (identity pass will suffix ids, not dedupe content).
  4. Send refresh.pull_to_refresh when the gesture should exist. Omit refresh when it should not.
  5. Do not paginate by mutating append_sections from an api tap unless the user asked for a discrete “load related.” Infinite lists belong on has_more.
  6. Keep widget ids unique across the merged document, not only within one HTTP response.

What the view does not do#

FlowPageView does not ask you for a page number. It does not encode ?page=2. It does not keep a Swift cursor beside postback. Hosts that already have a cursor type should serialize it into the JSON they put in pagination.postback on the way down, then decode it again in the loader on the way up if they must. Many backends never need the client to decode it at all.

There is no framework prefetch of page three. A host may call loadPage for the next cursor from its own logic. PageStore will not apply that fetch unless it went through loadNextPage or you apply mutations yourself.

Refresh versus pagination in the loader#

Use .refresh to bypass cache and to tell the backend you want a new first page. Use .nextPage to send postback and receive more sections. Mixing them (refresh that accidentally appends, or next-page that returns a full document you then replace by hand) fights PageStore. Let append stay append.

isLoadingMore gates overlapping pagination. Rapid scroll to the sentinel will not stack five next-page calls. A cancelled pageTask is replaced. If your loader ignores cancellation, you still burn the radio; the store will drop a response whose postback no longer matches.

Empty next-page documents: if sections is empty and has_more remains true, the sentinel will fire again. Either set has_more false or return at least one section. An empty successful next page is how you build a tight loop.

Refresh that returns zero widgets sets .empty, not .loaded with a blank list. That is a product choice on the backend. If you meant “keep the old page because the new one is empty,” the loader should throw or return the previous bytes, because the store will not merge refresh into the old model. Refresh replaces. Pagination appends.

Where this sits#

Page envelope names the fields. PageLoader: the networking seam names the kinds. Stable widget identities names why merged ids matter. Native refreshable and a native sentinel are still SwiftUI, not a WebView infinite scroller.

Opaque postback. Sentinel when has_more. Refresh keeps content during the round trip. Failed pagination does not blank the page.

The Flow-UI mark: two nodes joined through a filled centre

The backend describes widgets and cursors. PageStore appends and refreshes. The host still fetches.

More on the blog.

Read the source

Flow-UI is MIT licensed. The schema, renderer and starter widgets live on GitHub.

GitHub

Get started with Flow-UI

Install the Swift package, register a widget, and render a page from JSON.

Get started