Skip to content

6eero/nextjs-boilerplate

Repository files navigation

Next.js Boilerplate πŸš€

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.


πŸ”‘ Key Features

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

πŸ“‚ Project Structure

.
β”œβ”€β”€ 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

βš™οΈ Flow Overview

Each async operation follows a predictable lifecycle:

Component β†’ Task β†’ Action β†’ Reducer β†’ State β†’ UI
  1. Task (tasks.ts)

    • Dispatches START action.
    • Calls endpoint.
    • Dispatches SUCCESS or FAIL.
  2. Actions (actions.ts)

    • Define START, SUCCESS, FAIL action creators.
  3. Reducer (reducer.ts)

    • Updates state.data, state.loading, state.error consistently.
  4. State (context/App.tsx)

    • Global state exposed via Context API.
  5. UI (components/)

    • Components read loading, error, and data to render feedback.

πŸ— Adding a New Module

To add a new module (e.g., user):

  1. Create a folder: modules/user/

    • actions.ts β†’ define async actions.
    • reducer.ts β†’ handle lifecycle updates.
    • User.tsx β†’ optional UI entrypoint.
  2. Create api/User/

    • endpoint.ts β†’ encapsulate API calls.
    • tasks.ts β†’ define async tasks calling endpoints + dispatching actions.
  3. Extend context/ to register new reducer if needed.

This guarantees that each feature is isolated but follows the same rules.

πŸ“– How to Use this Boilerplate

This repository is configured as a GitHub Template, so you can easily start new projects while preserving the full Git history:

  1. Click the green β€œUse this template” button at the top of the repository.

  2. Choose β€œCreate a new repository”.

  3. GitHub will generate a brand-new repo with all files and commit history from the boilerplate.

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

πŸ“– Example

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>
  );
}

βœ… Why This Boilerplate?

  • Enforces a clear architecture.
  • Avoids repetitive boilerplate across projects.
  • Simplifies onboarding for new developers.
  • Scales smoothly as app complexity grows.

About

A scalable Next.js boilerplate designed to bootstrap new projects with a clean and extensible architecture. πŸš€

Topics

Resources

Stars

Watchers

Forks

Contributors