How Micro-Frontends, a BFF and tRPC Fit Together
On this page
Three tools, three problems
This project combines the three courses before it. Each tool answers a different question:
- Micro-frontends answer *how do several teams ship one web app without waiting on each other?* Each team owns part of the UI and deploys it on its own schedule.
- A Backend for Frontend answers *where do the security tokens live, and who shapes data for the screens?* It signs users in, keeps tokens on the server and gives the browser one
HttpOnlycookie. - tRPC answers *how do the frontend and the BFF agree on the shape of every call?* The BFF's router type becomes the contract, checked by the TypeScript compiler.
Used together, they cover each other's gaps. Micro-frontends alone tempt every team to invent its own auth. A BFF alone still leaves hand-written fetch calls that drift from the server. tRPC alone has no story for security or team boundaries.
The TaskBoard platform
Over the next ten days you'll build TaskBoard: a task tracker whose web app is split into two micro-frontends owned by different teams, behind one BFF.
- The shell, owned by the platform team, draws the header, routes between micro-frontends and owns the session experience.
- The tasks micro-frontend, owned by the tasks team, lists and creates tasks and updates live.
- The account micro-frontend, owned by the account team, shows the profile and renames the user.
- The BFF, a Fastify 5 server, serves the shell and the import map, runs OpenID Connect sign-in, keeps sessions in Redis, and hosts the tRPC router at
/trpc. Each team owns its own sub-router inside it. - Downstream services, like the tasks API, accept only bearer tokens and never see the browser.
Everything the browser talks to lives on one origin, like https://app.taskboard.dev. That one decision removes CORS, keeps the session cookie a strict first-party __Host-Http- cookie, and makes the CSRF defense a single header check.
One request, end to end
Follow what happens when Ada adds a task:
- The tasks micro-frontend calls
trpc.tasks.create.mutate({ title }). TypeScript already checked thattitleis a string, because the micro-frontend imports the BFF'sAppRoutertype. - tRPC's batch link sends
POST /trpc/tasks.createwith thex-csrf: 1header. The browser adds the session cookie by itself. - The BFF's CSRF hook sees the header and lets the request through.
- tRPC's
createContextreads the cookie, loads the session from Redis and decrypts the tokens. - The
protectedProceduremiddleware confirms there's a session. Zod validates and trims the title. - The tasks team's procedure calls the tasks API with Ada's access token, refreshing it first if it's about to expire, and forwards the request id.
- The tasks API checks the token's signature, issuer and audience, then saves the task.
- The procedure publishes a "created" event through Redis, so every BFF instance can push it to Ada's other open tabs over Server-Sent Events.
- The typed result returns to the micro-frontend, and TanStack Query refreshes the list.
At no point did the browser hold a token, a hand-written type or a URL to an internal service.
Who owns what
The architecture only delivers independence if ownership is clear:
| Piece | Owner | Deploys |
|---|---|---|
| Shell, import map service, BFF host and auth | Platform team | Rarely |
tasks micro-frontend and tasks tRPC router | Tasks team | Often |
account micro-frontend and account tRPC router | Account team | Often |
| Tasks API | Tasks backend team | On its own schedule |
@taskboard/contract package | Platform team, reviewed by all | Rarely |
Notice that each product team owns a vertical slice: its micro-frontend *and* its router in the BFF. A team can add a field to a procedure and use it in its screen in one pull request.
The trade-offs you're accepting
Be clear about the costs before building:
- Coupling through types. The micro-frontends import the BFF's router type, so they share a repository, or at least a published types package, with the BFF. That's the price of end-to-end types, and why this project uses a monorepo.
- A shared BFF. One BFF serves both teams, so its host code needs a careful owner, or it becomes a monolith.
- More moving parts than a single app: an import map, Redis, an identity provider, several builds.
For a small app with one team, this is too much. It pays off when several teams build one product and want both independence and safety.
Check yourself
Micro-frontends let teams ship parts of the UI independently, the BFF keeps tokens on the server and shapes data, and tRPC gives the frontend and BFF a compiler-checked contract.
One origin removes CORS entirely, so there are no preflights and no risky CORS configuration. It lets the session cookie be a strict first-party __Host-Http- cookie, and it makes the CSRF defense a single header check, because a cross-site page can't add that header without permission the BFF never grants.
So a feature that needs a new field can be built end to end by one team, in one change, without waiting for another team.
Not yet. Its benefits, independent team deploys and per-team ownership, only matter with several teams, while its costs (an import map, Redis, an identity provider, several builds) start on day one. A single app with a BFF-style backend and tRPC gives most of the safety and type benefits, and can be split later.
The tokens arrive at the BFF in a server-to-server call during sign-in, are encrypted into Redis, and only leave it as an Authorization header on calls from the BFF to services. The browser holds only an HttpOnly session cookie, and no tRPC procedure returns tokens. There's simply no path that carries a token to the page.