A scalable Next.js boilerplate designed to bootstrap new projects with a clean and extensible architecture. It provides a well-defined pattern for state management, API interactions, error/loading handling, and internationalization.
- Next.js 13+ App Router with i18n support.
- Global state management with Context + Reducers.
- Async actions lifecycle (
START β SUCCESS β FAIL) for consistent state transitions. - Encapsulated API calls with normalized responses.
- Predefined components for error and loading handling.
- i18n-ready with message catalogs (
en,it, β¦). - Modular structure: easily add new features without breaking global architecture.
.
βββ api/ # Application-wide API calls and async tasks
β βββ App/
β βββ endpoint.ts # Encapsulated API calls (e.g. whoAmI)
β βββ tasks.ts # Async logic dispatching actions + endpoints
β
βββ app/ # Next.js App Router
β βββ [locale]/ # Locale-aware routes
β β βββ layout.tsx
β β βββ page.tsx
β βββ globals.css # Global styles
β βββ layout.tsx # Root layout
β
βββ components/ # Reusable UI components
β βββ error/
β β βββ ErrorBlock.tsx
β βββ layout/
β βββ Loading.tsx
β βββ ResourceLoader.tsx
β
βββ context/ # Application contexts
β βββ App.tsx # App context provider (state + dispatch)
β βββ Contexts.d.ts # Shared context types
β
βββ i18n/ # Internationalization utilities
β βββ navigation.ts
β βββ request.ts
β βββ routing.ts
β
βββ messages/ # Translations
β βββ en/
β β βββ common.json
β β βββ errors.json
β βββ it/
β βββ common.json
β βββ errors.json
β
βββ models/ # Data models (per feature/module)
β βββ app/
β
βββ modules/ # Business logic per feature
β βββ app/
β βββ actions.ts # Action creators (async lifecycle)
β βββ App.tsx # Feature-specific components
β βββ reducer.ts # Reducer managing feature state
β
βββ utils/ # Utilities and helpers
β βββ actions/
β β βββ functions.ts # Async action generator
β βββ types/ # Shared TypeScript types
β
βββ public/ # Static assets
βββ middleware.ts # Middleware (e.g. i18n routing)
βββ next.config.ts # Next.js configuration
βββ tsconfig.json # TypeScript configuration
βββ README.md
Each async operation follows a predictable lifecycle:
Component β Task β Action β Reducer β State β UI
-
Task (
tasks.ts)- Dispatches
STARTaction. - Calls endpoint.
- Dispatches
SUCCESSorFAIL.
- Dispatches
-
Actions (
actions.ts)- Define
START,SUCCESS,FAILaction creators.
- Define
-
Reducer (
reducer.ts)- Updates
state.data,state.loading,state.errorconsistently.
- Updates
-
State (
context/App.tsx)- Global state exposed via Context API.
-
UI (
components/)- Components read
loading,error, anddatato render feedback.
- Components read
To add a new module (e.g., user):
-
Create a folder:
modules/user/actions.tsβ define async actions.reducer.tsβ handle lifecycle updates.User.tsxβ optional UI entrypoint.
-
Create
api/User/endpoint.tsβ encapsulate API calls.tasks.tsβ define async tasks calling endpoints + dispatching actions.
-
Extend
context/to register new reducer if needed.
This guarantees that each feature is isolated but follows the same rules.
This repository is configured as a GitHub Template, so you can easily start new projects while preserving the full Git history:
-
Click the green βUse this templateβ button at the top of the repository.
-
Choose βCreate a new repositoryβ.
-
GitHub will generate a brand-new repo with all files and commit history from the boilerplate.
-
Clone your new repo and start building:
git clone git@github.com:6eero/nextjs-boilerplate.git my-new-project cd my-new-project npm install npm run dev
Inside a page or component:
"use client";
import { useAppContext } from "@/context/App";
import { useAppActions } from "@/api/App/tasks";
export default function HomePage() {
const { data, loading, error } = useAppContext();
const { onWhoAmI } = useAppActions();
return (
<div>
<button onClick={onWhoAmI}>Check Session</button>
{loading && <p>Loading...</p>}
{error && <p>Error: {String(error)}</p>}
{data?.name && <p>Welcome, {data.name}!</p>}
</div>
);
}- Enforces a clear architecture.
- Avoids repetitive boilerplate across projects.
- Simplifies onboarding for new developers.
- Scales smoothly as app complexity grows.