Catalog My learning Blog
Micro-Frontends, BFF and tRPC: Full-Stack TypeScript Project · Phase 1 · Lesson 1 · 5 min read

How Micro-Frontends, a BFF and tRPC Fit Together

On this page
  1. Three tools, three problems
  2. The TaskBoard platform
  3. One request, end to end
  4. Who owns what
  5. The trade-offs you're accepting
  6. Check yourself

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 HttpOnly cookie.
  • 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.

One origin serves the shell, the micro-frontends, sign-in and the tRPC API.
One origin serves the shell, the micro-frontends, sign-in and the tRPC API.
  • 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:

  1. The tasks micro-frontend calls trpc.tasks.create.mutate({ title }). TypeScript already checked that title is a string, because the micro-frontend imports the BFF's AppRouter type.
  2. tRPC's batch link sends POST /trpc/tasks.create with the x-csrf: 1 header. The browser adds the session cookie by itself.
  3. The BFF's CSRF hook sees the header and lets the request through.
  4. tRPC's createContext reads the cookie, loads the session from Redis and decrypts the tokens.
  5. The protectedProcedure middleware confirms there's a session. Zod validates and trims the title.
  6. 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.
  7. The tasks API checks the token's signature, issuer and audience, then saves the task.
  8. 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.
  9. 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:

PieceOwnerDeploys
Shell, import map service, BFF host and authPlatform teamRarely
tasks micro-frontend and tasks tRPC routerTasks teamOften
account micro-frontend and account tRPC routerAccount teamOften
Tasks APITasks backend teamOn its own schedule
@taskboard/contract packagePlatform team, reviewed by allRarely

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.

TipDraw this diagram for your own system before writing code, with an owner's name on every box. A box nobody's name fits is a box nobody will maintain.

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.