Next.js Clean Architecture Template ποΈ
β Stars are welcome π
π Report an issue
π Visit my site
β¬οΈ Clone this repo
git clone https://github.com/maurogioberti/next-clean-architecture.git
cd next-clean-architecture
π Install dependencies
npm install
π Run the app
npm run dev
This project is structured to align with Clean Architecture principles. This folder structure is designed to maximize separation of concerns and ensure testability, scalability, and maintainability.
Here's how it's organized:
project-root/
βββ src/
β βββ core/ # Clean Architecture Core Logic
β β βββ application/ # Use cases
β β β βββ get-message-usecase.ts --> Handles application-specific logic
β β βββ crosscutting/ # Shared utilities and cross-cutting concerns
β β β βββ injection/
β β β βββ DependencyInjectionContainer.ts --> Configures DI container and bindings
β β βββ domain/ # Business rules
β β β βββ events/
β β β β βββ MessageEvent.ts --> Represents a domain event for notifications
β β β βββ model/
β β β β βββ Message.ts --> Models a message entity
β β β β βββ User.ts --> Models a user entity
β β β βββ repository/
β β β β βββ MessageRepository.ts --> Interface for message repository
β β β βββ services/
β β β βββ MessageService.ts --> Interface for message service
β β βββ infrastructure/ # External service implementations
β β βββ repository/
β β β βββ MessageRepositoryImpl.ts --> Concrete implementation of MessageRepository
β β βββ services/
β β βββ MessageServiceImpl.ts --> Handles message service
β βββ app/ # Next.js UI and pages
β βββ layout.tsx --> Global layout
β βββ pages/ # Next.js pages
β β βββ page.tsx --> Page Home component (View)
β β βββ homeViewModel.ts --> Page Home UI business logic (ViewModel)
β βββ components/ # Reusable UI components
β βββ HomeBanner.tsx --> Example of a reusable banner component
βββ public/ # Static assets (e.g., favicon)
β βββ favicon.ico --> Example favicon
βββ di.ts # Dependency injection configuration
Clean Architecture makes testing simple:
- Isolated Layers: Test each layer independently (Domain, Application, Infrastructure).
- Mockable Dependencies: Easily replace services and repositories with mocks.
- Solid Coverage: Unit and integration tests for core; smoke tests suggested for UI.
Consistency is key! Here's the naming convention used in this project:
Layer | Example Filename | Convention | Purpose | Notes |
---|---|---|---|---|
Pages | page.tsx |
camelCase | UI entry points for each route | Represents Next.js pages |
Components | HomeBanner.tsx |
PascalCase | Reusable UI components | Stateless, reusable pieces |
ViewModels | homeViewModel.ts |
camelCase | UI business logic | Encapsulates state |
Domain Event | MessageEvent.ts |
PascalCase | Models domain-specific events | Example: Notification triggers |
Domain Model | Message.ts |
PascalCase | Defines business entities | Core entities for the domain |
Domain Repository | MessageRepository.ts |
PascalCase | Abstracts data access logic | Interface for persistence |
Domain Service | MessageService.ts |
PascalCase | Encapsulates domain-specific operations | Logic specific to the domain |
Application Use Cases | get-message-usecase.ts |
kebab-case | Implements application-specific workflows | Directly consumes repositories |
Infrastructure Repository | MessageRepositoryImpl.ts |
PascalCase | Implements domain repository interfaces | Connects to external services |
Infrastructure Service | MessageServiceImpl.ts |
PascalCase | Implements domain service interfaces | Example: Notifications backend |
This is a Clean Architecture implementation in Next.js, inspired by Damian Pumar and the amazing work at CodeScouts Academy.
Based on their approach to Clean Architecture, it does not use any of their libraries, as they are CSR-focused. Instead, we implemented our own methods to handle DI with SSR.
If youβre interested in Clean Architecture, check out their React Template.
Feel free to fork this repository and send PRs with improvements or fixes. Iβd love to hear your feedback or collaborate on cool ideas. Donβt hesitate to open an issue if something isnβt working as expected!
Released under MIT License by Mauro Gioberti.