Next.js Scope Rule Architect
1. Overview
This skill implements the Scope Rule for Next.js architecture, a non-negotiable principle for maintaining scalable codebases. It analyzes usage patterns to decide whether a component, hook, or utility should be Local (private to a feature) or Shared (available globally). It ruthlessly prevents "shared folder dumping" (premature abstraction).
2. Prerequisites & Context
- •Required Tools:
- •
grep_search: To count the number of times a component is imported across different features. - •
list_dir: To explore current component locations. - •
move_file: (Conceptual) To recommend moving files based on the rule.
- •
- •Environment: Any Next.js project structure.
- •Input:
- •Name of the file/component in question.
- •(Optional) Usage context (e.g., "I want to use the
ProductCardin the Cart page too").
3. Workflow
- •Identify Artifact: Pinpoint the specific component, hook, or type being discussed.
- •Analyze Usage:
- •Use
grep_searchto find all imports of this artifact throughout thesrc/appdirectory. - •Count distinct Route Groups or top-level Features consuming it.
- •Use
- •Apply Logic:
- •Usage Count = 1: The artifact MUST reside in that feature's private
_components(or_hooks, etc.) folder. - •Usage Count ≥ 2: The artifact MUST be moved to
src/shared/components(orsrc/shared/hooks, etc.).
- •Usage Count = 1: The artifact MUST reside in that feature's private
- •Recommend Action: Provide exact move commands or refactoring instructions.
4. Detailed Instructions & Rules
Critical Rules
- • Rule 1: 1 Feature = Local. If a component is used in only one feature (even if multiple times within that feature), it must stay in that feature's private folder.
- • Rule 2: 2+ Features = Shared. As soon as a second, distinct feature imports a component, it must be promoted to
src/shared. - • Rule 3: Never create a "Common" or "Utils" folder inside a feature. Use
_utilsor specific names like_components. - • Rule 4: When promoting to shared, always ensure the component is decoupled from feature-specific logic (e.g., it shouldn't import from
(auth)/...). - • Rule 5: Start Local. When creating a new component, always put it in the local feature folder first. Only move it later if needed.
Logic Matrix
| Usage Count | Location | Example Path |
|---|---|---|
| 1 Feature | Local Private | src/app/(shop)/shop/_components/product-card.tsx |
| 2+ Features | Global Shared | src/shared/components/product-card.tsx |
| 0 Features | Delete | (Ideally) |
5. Examples
Example 1: Promoting a Component
See examples/promote-component.md.