12.3 · Pagination, caching and conditional requests, and authentication refresh

A client is more than data(from:). Three problems decide whether it behaves well in production: getting all the data, not downloading it twice, and staying logged in without a stampede.

Mental model. Treat every response as data plus a policy: how to ask for the next page, how long it stays valid, and what to do when credentials expire.

Data and caching

Pagination with Codable, and HTTP caching with ETags.

  1. How do you decode and paginate an API response, and which pagination style is safest? 12.3.1 · L3
    Model each response with Decodable types and decode in one place (JSONDecoder, with a configured key and date strategy as covered in the Foundation topic). A…
  2. How does HTTP caching work with Cache-Control, ETag and conditional requests, and how does URLCache fit in? 12.3.2 · L3
    HTTP caching is driven by response headers. Cache-Control tells the client how long a response is fresh (max-age=60) and whether it may be stored at all…

Authentication

Tokens, refresh races and 401 loops.

  1. How do you handle access-token expiry, refresh races and 401 loops? 12.3.3 · L5
    A typical API issues a short-lived access token and a longer-lived refresh token. The client attaches the access token to each request (Authorization: Bearer…

More in Networking