Modh
  • Work
  • About
  • Let's talk
Let's talk
Agent Skills/React / Next.js/Route Colocation
React / Next.jsbeginner6 min

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 .playbook

One 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

LayerPurposeLocationWhen to Use
RepositoryDatabase CRUD_shared/repositories/ALL database operations
ServiceBusiness logic_shared/services/Complex multi-step operations
Server ActionEntry pointRoute actions.tsUser-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

LayerLocationAudiencePurpose
Human docsdocs/Developers reading and learningExplains how and why -- patterns, runbooks, standards
AI skills.claude/skills/AI coding agentsEnforces 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

TypeLocationWhen
Engineering patternsdocs/patterns/Reusable approach for building something
Operational runbooksdocs/runbook/<integration>/External system that needs debugging knowledge
User journeysdocs/cuj/End-to-end flow spanning multiple components
Coding standardsdocs/standards/Rule applying across the entire codebase
Internal guidesdocs/internal/Deployment procedures, environment setup
Route-level AI context<route>/AGENTS.md + CLAUDE.mdDirectory with enough complexity for AI context

In-Code AI Context Files

When a directory contains enough complexity that AI tools need context:

  1. Write content in AGENTS.md (source of truth)
  2. Create CLAUDE.md with just @AGENTS.md (Claude Code import)
  3. 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:

UsageLocationRationale
1 routeColocated in route directoryNo need to share
2 routesColocated, or shared if expansion is likelyUse judgment
3+ routes_shared/ directoryClearly shared infrastructure

When promoting to shared:

  1. Move the file to the appropriate _shared/ subdirectory
  2. Update all imports
  3. Ensure the module has a clear, single responsibility
  4. Add documentation if the module is non-trivial

New Feature Documentation Checklist

When building a significant new feature, create documentation in this order:

  1. In-code AGENTS.md + CLAUDE.md -- immediate AI context for the route/directory
  2. Pattern doc (docs/patterns/) -- if the feature introduces a reusable pattern
  3. User journey doc (docs/cuj/) -- if the feature is a multi-step flow
  4. Runbook (docs/runbook/) -- if the feature integrates an external system
  5. Skill (.claude/skills/) -- if the pattern has strict enforceable rules
  6. Update indexes -- add entries to relevant README.md files

Checklist for New Routes

  • page.tsx -- Server Component for data fetching
  • actions.ts or actions/ -- 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

WhatWhereRule
Database queries_shared/repositories/Always
Complex business logic_shared/services/3+ steps or external services
User mutations/fetchesRoute actions.ts or actions/Colocated with route
Route componentsRoute components/Default
Shared components_shared/components/3+ routes threshold
Route-specific typesRoute types.tsSingle route use
Shared types_shared/types/Multiple route use
Validation schemas_shared/validation/Always
Engineering patternsdocs/patterns/Reusable approaches
Operational runbooksdocs/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.js

E2E 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.js

Header 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
←Back to Agent Skills