PageLoader: the networking seam
Flow-UI does not fetch. Your PageLoader does. URLSession, GraphQL, a fixture file: all valid as long as they return a page document.
Flow-UI does not fetch. Your PageLoader does. URLSession, GraphQL, a fixture file: all valid as long as they return a page document. The protocol is one async method. The return type is Data. Decode, unknown widgets, mutations, and pull to refresh all sit above that byte blob.
This is the portability seam. The package has no HTTP client, no certificate pinning, no auth header helper, and no base URL. Installation adds the Swift package. Architecture names load as step one: PageStore calls the host loader for a page id. How to implement SDUI on iOS already said networking stays in the host. This article is the PageRequest kinds and what a correct loader must not forget.
API actions and page mutations is why .action exists. Pagination and refresh kinds are how PageStore reuses the same method rather than growing a second protocol.
The entire HTTP story is one function#
PageRequest carries pageID and kind. Kind is:
initialfor the first loadrefreshfor pull to refresh andrefresh_pagenextPage(postback:)for pagination, wherepostbackis whatever the previous envelope put inpagination.postback, echoed untouchedaction(JSONValue)for anapiaction, where the value is the action’s raw object
A loader that ignores kind and always GETs /pages/{id} will break pagination and api. A loader that inspects kind and still returns a PageResponse blob for .initial and .refresh, a next-page document for .nextPage, and an ActionResponse blob for .action is doing the job.
Flow-UI never calls URLSession. Image loading is a separate host seam (FlowImageLoader). Do not look for a hidden fetch inside PageStore beyond loader.loadPage.
Fixtures are first-class#
A test loader that maps pageID to Bundle JSON is a valid PageLoader. The playground’s paste-JSON path is the same idea: bytes in, PageModel out. GraphQL hosts POST a query and extract the envelope as Data before returning. gRPC hosts decode protobuf to JSON Data if that is what you store, or you write a loader that returns JSON you synthesized. The package only needs Data that FlowDecoder can parse as PageResponse or ActionResponse.
What PageStore will call#
loadInitial() sets state to .loading and fetch(.initial). refresh() resets WidgetStateStore and fetch(.refresh) without first blanking the loaded page. loadNextPage() builds .nextPage(postback: current.pagination?.postback) when hasMore is true and a load is not already running. performAction is .action(payload).
fetch cancels the previous page fetch so a slower response does not overwrite a newer one. Pagination uses a separate pageTask and, on failure, swallows the error so a healthy page stays loaded. That failure policy is the next article’s territory; the loader should still throw when the network fails so the store can tell the difference.
Map HTTP errors to Error. PageStore.friendlyMessage prefers LocalizedError.errorDescription, else a generic retry string. Decoding errors get describeDecodingError. Do not return 200 with an HTML error page and expect a pretty failed state. Return thrown errors or valid JSON.
Headers belong here#
Auth cookies, experiment cohorts, and optional capability headers (supported types, a host max_schema_version convention) attach in the loader. The package will not send them. Unknown widgets and schema versioning described capability headers as host policy. This is the function that actually writes the header.
Retries, timeouts, and cancellation: honor Task.isCancelled after awaits so a superseded fetch does not keep working. The store cancels; your loader should be cancellation-aware or you will waste radio on a page the user already left.
What not to put in the loader#
Do not decode PageModel inside the loader and then re-encode to Data. Return the network bytes. The store decodes off the main actor with the registry and a fresh DecodeDiagnostics. Double decode loses diagnostics and risks diverging JSONDecoder configs.
Do not cache inside a type you pretend is “Flow-UI cache.” Caching is the next article: still your loader, still not the package.
Do not call PageStore.apply from the loader. The loader returns bytes. Handlers apply mutations after decode.
Do not catch every error and return an empty Data(). That becomes a decode failure that looks like a schema bug. Throw.
Hosting shapes that work#
SwiftUI app: PageStore(pageID: "home", loader: APILoader(), registry: registry) then FlowPageView(store:).
UIKit app: the same store, then FlowHostingController. Push it. Widgets remain SwiftUI.
Multiple page ids: one loader instance, many stores. The loader branches on request.pageID.
Offline demo: loader returns bundled JSON for every kind you support, or throws a typed offline error for .action if mutations cannot be fake.
Architecture is the diagram. Native views still sit on the other side of decode, which is native SDUI in SwiftUI. The loader is not a WebView. It is your existing client.
Checklist before you ship the seam#
- Implement
PageLoaderon an actor or struct that isSendable. - Switch on
kind. Handlepostbackas opaque: echo it in the next HTTP body or query as the backend specified. Do not parse it in the app unless you own both sides and still treat it as optional. - Return raw page JSON for initial, refresh, and next page. Return raw action JSON for
.action. - Throw on transport failure. Do not silently empty.
- Honor cancellation.
- Put auth and capability headers here.
- Use a fixture loader in tests.
Kind-specific mistakes#
.initial and .refresh often share a URL and differ only by cache policy. That is a valid loader design. Returning cached bytes for .refresh defeats pull to refresh. Honor the kind even when the path is the same.
.nextPage must send postback exactly. Pretty-printing the JSON, dropping unknown keys, or wrapping it as { "postback": ... } when the backend expected the raw cursor will desync the list. Treat JSONValue as opaque. If you must log it, log the serialized bytes, not a reconstructed dictionary you “cleaned.”
.action is a different response shape. Branch on kind before you parse. A single decode(PageResponse.self) for every request will break mutations. The store decodes pages and action responses with different types. Your job is bytes that match the kind.
Timeouts should differ. An initial home fetch can wait longer than a pagination fetch. Pagination failure is swallowed by PageStore; a 30-second hang still freezes the sentinel. Cancel promptly.
Do not implement PageLoader on a class that also owns PageStore. The store calls the loader. A cycle makes tests and cancellation painful. One loader instance, many stores, as the hosting section said.
Where this sits#
Installation gets the package into the target. Architecture places the loader at the edge. API actions and page mutations is one kind. Caching and pagination are host policies that still return Data from this same method.
One function. Four kinds. Zero HTTP inside Flow-UI.
Read next#
loadPage returns Data. The host fetches. The store decodes.
Related documentation
Get started
Installation
Add the Flow-UI server driven UI package to an iOS or macOS app with Swift Package Manager. Requires iOS 17, no third party dependencies.
Concepts
Architecture
How Flow-UI turns a JSON page into native SwiftUI: decode through an open registry, contain failures, render widgets, and dispatch actions as data.
Related articles
- Production6 min read
Decode diagnostics in production
Surface DecodeDiagnostics in debug overlays and logs. A malformed widget should be a telemetry event, not a blank screen.
- Production6 min read
Swift 6 and MainActor widgets
WidgetView is MainActor. Swift 6 language mode is the default for Flow-UI. Register widgets that are safe to render on the main actor.
- Production6 min read
Accessibility in server driven SwiftUI
Native widgets inherit VoiceOver. The schema still has to send alt text, button traits, and type that scales.
More on the blog.
Read the source
Flow-UI is MIT licensed. The schema, renderer and starter widgets live on GitHub.
GitHubGet started with Flow-UI
Install the Swift package, register a widget, and render a page from JSON.
Get started