Skip to content

Describe what a route's loaders resolve to - #812

Open
pleek91 wants to merge 1 commit into
add-loaderfrom
route-data
Open

Describe what a route's loaders resolve to#812
pleek91 wants to merge 1 commit into
add-loaderfrom
route-data

Conversation

@pleek91

@pleek91 pleek91 commented Aug 3, 2026

Copy link
Copy Markdown
Member

Description

Fifth step of #805, stacked on #811. Types only.

RouteDataOf combines the loaders of every match, so a child sees an ancestor's data as its own, and flattens to the loader's data directly when the only loader in the tree is unnamed:

const user = createRoute({ name: 'user', path: '/user/[id]' })
  .addLoader(async (route) => api.getUser(route.params.id))

// data -> Promise<User>

const posts = createRoute({ parent: user, name: 'user.posts', path: '/posts' })
  .addLoader(async (route) => api.getPosts(route.params.id), { name: 'posts' })

// data.default -> Promise<User>
// data.posts   -> Promise<Post[]>

Data is always a promise, even for a synchronous loader. Loaders do not hold up rendering, so a component reading data cannot be given a settled value — making that uniform is cheaper than making it conditional.

It is carried by ResolvedRouteWithData rather than by ResolvedRoute, because only the route being navigated to has data. A route the router merely resolved — router.resolve, router.find, the to/from a hook receives — is not being loaded, so data on it could only be a promise that never settles. #814 gives it to the two surfaces that do have it: the current route, and a props getter's route.

Also worth a look: the unregistered case types data as Promise<unknown> | Record<string, Promise<unknown>> | undefined rather than just a record, because a route whose data is a bare promise has to satisfy the constraint.

@pleek91 pleek91 changed the title Expose what a route's loaders resolve to as route.data Describe what a route's loaders resolve to Aug 3, 2026
RouteDataOf combines the loaders of every match, so a child sees an
ancestor's data as its own, and flattens to the loader's data directly when
the only loader in the tree is unnamed.

Carried by ResolvedRouteWithData rather than by ResolvedRoute: only the
route being navigated to has data, so a route the router merely resolved
would carry a promise that could never settle.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant