GuideBeginner~10 min
Loading...
GuideBeginner~10 min
Loading...
Every route can load data on the server in its page.rs handler and hand it to the
React component as typed props. This guide goes deeper on that pattern: fetching,
composing multiple sources, handling failures, and showing a loading state while
it resolves.
If you haven't built a route yet, start with Build your first Ossido app.
A route's page.rs runs a #[handler] on the server, and its return value becomes
the page's props (see Page & Layout Handlers).
Long-lived clients - an HTTP client, a database pool - live in application state and
are injected by a matching parameter name (see
Dependency injection):
Because the handler runs on the server, the data is baked into the initial HTML -
there's no client fetch on first paint (see
Request lifecycle). To run
independent fetches concurrently, reach for the re-exported
ossido::tokio::join!.
The page component is typed by path, so its props match the handler exactly (see TypeScript integration):
Add a loading.tsx next to the route and Ossido renders it as a <Suspense>
fallback while the handler's data resolves - on first load and on client
navigation into the route:
A loading.tsx covers its route and everything nested under it, so you can put a
coarse fallback high in the tree and finer ones deeper.
For expected failures - a missing record, an upstream error - return a Response
with the right status rather than panicking (see
Returning data and
Error handling):
A genuinely unexpected error can just panic - Ossido catches it and renders the error page instead of crashing the server.
After something changes the data a page was rendered with, refresh the current
route in place with refetchProps() from useRouter. The page stays on screen (no
loading.tsx flash) and isRefetching reflects the pending state (see
Refetching props):