Route Colocation
Guide code organization, folder structure, and route architecture following colocation-first patterns. Use when creating routes, deciding file placement, organizing imports, or structuring documentation. Enforces the 3+ routes sharing threshold, actions folder pattern, and documentation placement rules.
Install this skill
git submodule add https://github.com/modh-labs/playbook.git .playbookOne submodule installs all 17 skills. Reference .playbook/skills/route-colocation/SKILL.md from your AGENTS.md.
Route Colocation Skill
When This Skill Activates
This skill automatically activates when you:
- Create new routes or features
- Decide where to place new code
- Organize components, actions, or types
- Work with shared directories
- Add or update documentation for a feature
Core Philosophy
Colocation first, share when necessary.
Code lives with the route that uses it until it needs to be shared. The threshold for sharing is 3+ routes or 2 routes with clear expansion potential.
Directory Structure
app/
|-- (protected)/ # Authenticated routes
| |-- [route]/ # Example: calls, leads, dashboard
| |-- page.tsx # Server Component (data fetching)
| |-- actions.ts # Server actions (1-2 actions)
| |-- actions/ # Server actions folder (3+ actions)
| | |-- create-item.ts
| | |-- delete-item.ts
| |-- components/ # Route-specific components
| |-- loading.tsx # Skeleton (matches UI layout)
| |-- error.tsx # Error boundary with retry
| |-- AGENTS.md # Route documentation (AI context)
| |-- CLAUDE.md # Contains "@AGENTS.md" (Claude Code import)
|
|-- (public)/ # Public routes
|
|-- api/webhooks/ # ONLY for external webhooks
|
|-- _shared/ # Shared code (3+ routes)
|-- components/ # Shared UI components
|-- lib/ # Utilities, clients
|-- repositories/ # Data access layer
|-- services/ # Business logic
|-- types/ # TypeScript types
|-- validation/ # Validation schemas
When to Use What
Repository vs Service vs Action
| Layer | Purpose | Location | When to Use |
|---|---|---|---|
| Repository | Database CRUD | _shared/repositories/ | ALL database operations |
| Service | Business logic | _shared/services/ | Complex multi-step operations |
| Server Action | Entry point | Route actions.ts | User-triggered mutations |
User Click -> Server Action -> Service (if complex) -> Repository -> Database
Decision Tree
"Where should this code live?"
Is it a database query?
-> _shared/repositories/[entity].repository.ts
Is it complex business logic (multi-step, external services)?
-> _shared/services/[domain].service.ts
Is it a user-triggered mutation or fetch?
-> Route's actions.ts or actions/
Is it a UI component?
-> Used by 1-2 routes? -> Route's components/
-> Used by 3+ routes? -> _shared/components/
Is it a type definition?
-> Route-specific? -> Route's types.ts
-> Shared across routes? -> _shared/types/
Is it validation?
-> _shared/validation/[route].schema.ts
Documentation Placement
Two-Layer System
| Layer | Location | Audience | Purpose |
|---|---|---|---|
| Human docs | docs/ | Developers reading and learning | Explains how and why -- patterns, runbooks, standards |
| AI skills | .claude/skills/ | AI coding agents | Enforces rules mechanically -- MUST/NEVER directives |
These layers often cover the same topic but serve different consumers. When you add a new pattern, consider whether BOTH need updating.
Where to Put Documentation
| Type | Location | When |
|---|---|---|
| Engineering patterns | docs/patterns/ | Reusable approach for building something |
| Operational runbooks | docs/runbook/<integration>/ | External system that needs debugging knowledge |
| User journeys | docs/cuj/ | End-to-end flow spanning multiple components |
| Coding standards | docs/standards/ | Rule applying across the entire codebase |
| Internal guides | docs/internal/ | Deployment procedures, environment setup |
| Route-level AI context | <route>/AGENTS.md + CLAUDE.md | Directory with enough complexity for AI context |
In-Code AI Context Files
When a directory contains enough complexity that AI tools need context:
- Write content in
AGENTS.md(source of truth) - Create
CLAUDE.mdwith just@AGENTS.md(Claude Code import) - Keep it brief and focused on what's unique about this directory
When to Create a Matching Skill
Create or update a .claude/skills/ skill alongside human docs when:
- The pattern has strict rules (MUST/NEVER) that an AI agent should enforce
- Violations are common and caught repeatedly in code review
- The pattern involves code generation (scaffolding, boilerplate)
Do NOT create a skill when:
- The doc is purely informational (architecture overview, runbook)
- The topic is too broad to enforce mechanically
- It's a one-off guide (deployment steps, investigation notes)
Actions Folder Pattern
When a route has 1-2 server actions, use a single file:
route/
|-- page.tsx
|-- actions.ts # Contains 1-2 actions
When a route has 3+ server actions, use a folder:
route/
|-- page.tsx
|-- actions/
|-- create-item.ts # One action per file
|-- update-item.ts
|-- delete-item.ts
Each action file exports a single server action function. No barrel files.
The 3+ Routes Threshold
This is the key decision boundary for code organization:
| Usage | Location | Rationale |
|---|---|---|
| 1 route | Colocated in route directory | No need to share |
| 2 routes | Colocated, or shared if expansion is likely | Use judgment |
| 3+ routes | _shared/ directory | Clearly shared infrastructure |
When promoting to shared:
- Move the file to the appropriate
_shared/subdirectory - Update all imports
- Ensure the module has a clear, single responsibility
- Add documentation if the module is non-trivial
New Feature Documentation Checklist
When building a significant new feature, create documentation in this order:
- In-code
AGENTS.md+CLAUDE.md-- immediate AI context for the route/directory - Pattern doc (
docs/patterns/) -- if the feature introduces a reusable pattern - User journey doc (
docs/cuj/) -- if the feature is a multi-step flow - Runbook (
docs/runbook/) -- if the feature integrates an external system - Skill (
.claude/skills/) -- if the pattern has strict enforceable rules - Update indexes -- add entries to relevant README.md files
Checklist for New Routes
-
page.tsx-- Server Component for data fetching -
actions.tsoractions/-- Server actions -
components/-- Route-specific components -
loading.tsx-- Skeleton matching UI layout -
error.tsx-- Error boundary with retry -
AGENTS.md+CLAUDE.md-- Route documentation for AI context - Repository in
_shared/repositories/if new entity - Validation schema in
_shared/validation/
Quick Reference
| What | Where | Rule |
|---|---|---|
| Database queries | _shared/repositories/ | Always |
| Complex business logic | _shared/services/ | 3+ steps or external services |
| User mutations/fetches | Route actions.ts or actions/ | Colocated with route |
| Route components | Route components/ | Default |
| Shared components | _shared/components/ | 3+ routes threshold |
| Route-specific types | Route types.ts | Single route use |
| Shared types | _shared/types/ | Multiple route use |
| Validation schemas | _shared/validation/ | Always |
| Engineering patterns | docs/patterns/ | Reusable approaches |
| Operational runbooks | docs/runbook/ | External system debugging |
| AI enforcement rules | .claude/skills/ | Strict MUST/NEVER rules |
Related Skills
Build-Time Reads
Make the build see the same inputs as runtime, and read files in a shape the bundler can trace. Activates when a Next.js root layout, static or partially prerendered page reads process.env; when adding an env var to a Turborepo monorepo (turbo.json env, passThroughEnv, strict mode); when a build log warns that variables are "missing from turbo.json" or that dynamic filesystem access "causes tracing of the whole project"; or when prebuilt pages and per-request pages disagree about a config value.
React / Next.jsE2E Testability & Accessible UI
Write resilient Playwright e2e tests and testable React UIs. Use when writing e2e specs, reviewing flaky tests, building UI components, or adding accessibility attributes. Enforces semantic locators, accessible names, web-first assertions, and Page Object fixtures.
React / Next.jsHeader Yields to the Task Surface
A header that hides on scroll down and comes back on scroll up must stay hidden while the page's main task surface (an embedded scheduler, checkout, form or player) sits under it. It measures where it would rest, ignoring its own hide transform, and checks for overlap with marked elements on each scroll. Also: how to test "stays hidden" against a scroll handler throttled to animation frames. Use when building or fixing a hide-on-scroll or auto-hide header, when a sticky or floating header covers a form, iframe or embed after scrolling up, when an anchor jump lands content under the header, or when writing a Playwright test that asserts something does NOT appear after a scroll.
React / Next.js