Skip to content

feat(router): add route loaders via static loader() method#255

Merged
mohamedmansour merged 1 commit intomainfrom
feat/router-2-loaders
Apr 20, 2026
Merged

feat(router): add route loaders via static loader() method#255
mohamedmansour merged 1 commit intomainfrom
feat/router-2-loaders

Conversation

@mohamedmansour
Copy link
Copy Markdown
Contributor

@mohamedmansour mohamedmansour commented Apr 20, 2026

Description

Why: Components need a way to fetch their own data instead of relying on server-provided state — WebSocket dashboards, custom APIs, client-side caches all need this. TanStack and Remix both have loader APIs; we had no equivalent.

What: Add static loader({ params, query, signal }) on component classes as the structured replacement for the removed stateless attribute. The router orchestrates loader execution, error handling, and abort signals.

How:

  • resolveLoaders() runs pre-commit (before view transition) so loader results are available synchronously during DOM update.
  • Loaders also run on SSR bootstrap for SSR/SPA parity — the same data source is used regardless of entry point.
  • On failure, the loader is skipped with console.warn and the component falls back to data.state from the server partial.
  • AbortSignal tied to navigation cancels in-flight loaders when the user navigates away.
  • Export RouteLoaderContext type from @microsoft/webui-router.

Test Plan

  • pnpm test in webui-router — 55 unit tests pass (6 new loader tests)
  • Unit tests cover: loader invocation, fallback on failure, abort signal respect, state override
  • Docs updated: routing.md loader section, ai.md route attribute reference, router README

Checklist

  • static loader() method on component class
  • Pre-commit execution (before view transition)
  • SSR bootstrap parity
  • Graceful fallback on loader failure
  • AbortSignal cancellation
  • RouteLoaderContext type exported
  • routing.md, ai.md, router README updated

Stack: 2/6 — depends on #254

mcritzjam
mcritzjam previously approved these changes Apr 20, 2026
Base automatically changed from feat/router-1-preload-keepalive to main April 20, 2026 22:26
@mohamedmansour mohamedmansour dismissed mcritzjam’s stale review April 20, 2026 22:26

The base branch was changed.

Components can define a static loader({ params, query, signal }) method
to fetch data from a custom source instead of using server-provided state.

- Loaders run pre-commit (before view transition) for synchronous DOM update
- Loaders run on SSR bootstrap too for SSR/SPA parity
- On failure, falls back to server data.state with a console warning
- AbortSignal tied to navigation cancels in-flight loaders
- Export RouteLoaderContext type from @microsoft/webui-router
- 6 new unit tests (54 total, all passing)
- Update routing docs, ai.md, and router README

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mohamedmansour mohamedmansour merged commit acce3b1 into main Apr 20, 2026
14 checks passed
@mohamedmansour mohamedmansour deleted the feat/router-2-loaders branch April 20, 2026 22:32
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.

2 participants