跪拜 Guibai
← All articles
Frontend Engineering · Architecture · Frontend Framework

For AI Agents, Organize Frontend by Page, Not by Type: Vertical Slices Cut File Reads by 62.5%

By ENCI ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

When AI agents do the editing, file layout becomes a direct cost metric: every extra file read consumes context and invites cross-page contamination. Shrinking the modification radius to one directory plus one shared file is a concrete way to reduce both token spend and the chance an AI copies patterns from the wrong page.

Summary

The argument starts with a concrete symptom: changing one button on an orders page means the AI must wander through routes, components, hooks, API, and styles directories, because horizontal type-based layering gives no signal about which files belong together. AI has no memory of the codebase; it only knows what it reads, so scattered files become a context-window cost, not just a navigation annoyance.

Vertical slicing reverses the axis: one URL, one directory containing the page's structure, styles, and tests, with pages forbidden from importing each other. The shared layer shrinks to a whitelist of three items with intrinsic single-source-of-truth properties: the API service layer, theme variables, and global toast. A router table is the sole registry connecting URLs to directories.

The article puts numbers on the trade-off: for a 20-page repo with 5 files per page, a type-layered change reads 8 files (~9600 tokens), while a sliced change reads 3 (~3600 tokens), a 62.5% reduction in context. It openly admits code volume rises ~30% short-term due to duplication, but argues AI cost tracks per-task read volume, not repo size, and that page-local duplication replaces O(page-count) regression risk with O(1) deletion risk. It closes with five pitfalls and clear criteria for when slicing is or isn't worth it.

Takeaways
— Traditional type-based layering (components, styles, hooks, utils) forces an AI to read across 4-5 directories for a single page change; vertical slicing puts page structure, page CSS, and page JS in one page directory.
— Pages in a vertical slice never import each other; sharing is limited to a whitelist of three categories: API service layer, theme variables, and global toast.
— The router table is the only place that maps URLs to page directories; adding or deleting a page means adding or deleting one directory and one route line.
— For a 20-page repo with 5 files per page, a type-layered change reads 8 files (~9600 tokens) versus 3 files (~3600 tokens) under slicing, a 62.5% reduction.
— Short-term code volume increases about 30% (e.g., 9000 to 12000 lines) because shared components are duplicated per page; the author argues AI cost depends on per-task read volume, not total repo lines.
— Deleting a page in a vertical slice is atomic: remove one directory and one route entry, leaving no orphaned styles, hooks, or components.
— The whitelist boundary is enforced with the rule of three: write once, copy twice, abstract on the third use; anything else entering shared must answer three questions about cross-page usage.
— Five pitfalls: forgetting theme and toast are shared; treating Button/Table/Modal as shared components; letting page-local utils leak across pages; letting router table and directories drift apart; and style priority conflicts requiring a fixed order of theme variables, page styles, then runtime overrides, with only class selectors and no !important.
— Vertical slicing suits 10+ pages with distinct business logic when AI is the primary editor; it's not recommended for 3-5 pages, design-system-centric projects, or teams that need strict human layered review.
Conclusions

The 8-to-3 reduction is a back-of-envelope estimate built on explicit assumptions, but its direction holds: the real variable is how many directories a single page spans, so the benefit grows with page count and file scattering and shrinks for small or tightly-coupled pages.

The article reframes code duplication as a feature: when the editor is a context-window LLM, duplicating page-local code replaces O(page-count) regression risk with O(1) read cost, which flips the usual human-centric DRY trade-off.

The whitelist of shared layers (API, theme, toast) is justified not by convention but by inherent singleton properties: a backend contract, site-wide visual tokens, and a single on-screen notification each have no natural page owner.

The honest admission that total lines rise ~30% directly contradicts the instinct that DRY is always better; the argument rests on the premise that AI is the primary modifier, which is a specific organizational decision, not a universal law.

Concepts & terms
Vertical Slice Architecture
Organizing code by business feature or page rather than by technical type: each slice (e.g., one URL) contains its own structure, styles, logic, and tests, so a change stays within one directory. Also called feature-based organization.
Context Window
The limited amount of text a language model can consider at once. For AI coding, any file that isn't read into the context is effectively unknown, so file-read count directly drives cost and reliability.
Context Pollution
When an AI reads unrelated files from other pages alongside the one it's changing, its attention gets diluted and it may copy patterns from the wrong page, increasing cross-page bugs.
Rule of Three
A refactoring heuristic: write the logic once, copy it on second use, and only abstract it on the third. In AI-driven development it's treated as a cost discipline because each shared file adds a read to every related page's change.
Colocation
Placing related code (e.g., a page's CSS next to its JS in the same directory) so that the filesystem structure reflects ownership, reducing lookups and accidental cross-page effects.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗