Skip to content
Marcel Lefter

Engineering LogArchitecture

Feature-Sliced Design in a monorepo

The front-end setup behind an estimated 5x lower infrastructure cost and 90% fewer API integration bugs.

Marcel Lefter5 min read
  • Architecture
  • Monorepo
  • TypeScript
  • React

Each front-end team wrote and structured its projects in its own way, and nothing was standard. With many projects running, getting into one to add a feature or fix a task was chaos. The fix was boring, well-understood structure, applied the same way in every project. When you switch context, it takes less mental energy to find where to work, and more energy stays for the work itself.

The structure has been used in about 30 projects for more than 5 years, by over 15 people, and it is still in use.

The layers

#

Feature-Sliced Design splits a front-end into layers by responsibility. One rule does most of the work: a layer can only import from the layers below it, and slices on the same layer never import each other.

LayerOwnsCan import
appProviders, routing, global stylesall layers below
pagesRoute-level compositionwidgets and below
widgetsLarge self-contained UI blocksfeatures and below
featuresUser actions with business valueentities, shared
entitiesBusiness objects such as user, invoice, ordershared
sharedUI kit, API client, utils, confignothing

Monorepo structure

#

Before this, each project lived in its own repo and was deployed as its own microservice. That was hard to maintain, and keeping every service up cost money. One repo, deployed from one place, cut that cost, which is where the infra saving comes from. It also meant no package versions and updates to manage across repos, and faster development.

Each app keeps its own layers. Anything shared, like the design system, the generated API clients and the tooling, lives in packages.

repo
.
├── apps/
│   ├── web/src/
│   │   ├── app/
│   │   ├── pages/
│   │   ├── widgets/
│   │   ├── features/
│   │   ├── entities/
│   │   └── shared/
│   └── admin/            # same layers
└── packages/
    ├── ui/               # design system and tokens
    ├── api/              # generated API clients
    └── config/           # eslint, tsconfig

Public APIs, not deep imports

#

Every slice exposes a single index.ts. Everything else stays private, which is what makes refactoring safe.

entities/invoice/index.ts
export { InvoiceCard } from "./ui/InvoiceCard";
export { useInvoice } from "./model/useInvoice";
export type { Invoice } from "./model/types";
features/pay-invoice/ui/PayButton.tsx
// avoid: reaches into the sliceimport { useInvoice } from "@/entities/invoice/model/useInvoice";// prefer: the public APIimport { useInvoice } from "@/entities/invoice";

A TypeScript API generator

#

Hand-written API clients drift from the backend. The backend and front-end teams were separate, so when the API changed without a heads-up, bugs followed. The generator, Hey API (@hey-api/openapi-ts), reads the backend's Swagger spec and generates the endpoints and types every time we start work and on every deployment, so everything stays up to date. If a field becomes required, gets renamed or is removed, the build fails and we see it at once, before it reaches production. Generating typed clients removed the manual inconsistencies and turned most integration bugs into compile errors.

$ pnpm --filter @acme/api generate

Boring architecture wins.

P/02, How I work

What it changed

#
  • ~5xlower infrastructure cost
  • ~90%fewer API integration bugs
  • ~50%less dev effort per project

These figures are my own estimates, not measurements.

The 50% came from SaaS boilerplate templates and a Figma-to-token pipeline. New projects start from a working base instead of an empty repo.

Before this, nothing was standard. Each project was built a little differently, the designer worked however they saw fit, and every UI meant writing styles from scratch. Types and endpoints were also written by hand. Then we bought a Figma design-system package based on Ant Design. Designers built UI from its components directly, and the front-end team pulled the design tokens into the project, so the whole design system existed in code from the start. What was left was building pages and features and connecting them to the API, and with the generated types and endpoints there was nothing to write by hand there either. Because we moved faster, the front-end stayed ahead and waited for the API, not the other way around.

Trade-offs

#

A monorepo has its own risks. Everything is deployed from one place, so if the service fails, the whole app can stop working, and a change to shared code can affect every app at once. What it gave us was faster development: everything in one repo, no package versions and updates to manage, and one place to deploy.

Newcomers got the standards for how to code and how to structure a feature, so they could execute tasks instead of thinking about structure.

Out of the chaos came order: projects still follow one structure and logic, and the front-end is built fast. I would do it again, and I'd review the structure more often to keep improving efficiency.

Subscribe

Get the next entry by email.

RSS feed
Written by
Marcel LefterTechnical Product Lead, SkalX

13 years in software, front-end first. I've led teams of up to 15 and now take products from client need to production.

Open to founding and product engineer roles