Architecture

Understand the architecture and structure of the Antimetal boilerplate.

Architecture Overview

This document provides an overview of the architecture and design principles of the Antimetal boilerplate.

Core Technologies

The Antimetal boilerplate is built on the following core technologies:

  • Next.js: The React framework for production, providing server-side rendering, static site generation, and the App Router.
  • React: A JavaScript library for building user interfaces.
  • TypeScript: A strongly typed programming language that builds on JavaScript.
  • Tailwind CSS: A utility-first CSS framework for rapidly building custom designs.
  • NextAuth.js: A complete authentication solution for Next.js applications.

Project Structure

The project follows a modular structure to keep the codebase organized and maintainable:

antimetal/
├── public/            # Static assets
├── src/
│   ├── app/           # App router pages and layouts
│   │   ├── (auth)/    # Authentication-related pages (login, register)
│   │   ├── (site)/    # Main site pages
│   │   ├── api/       # API routes
│   │   └── dashboard/ # Dashboard pages (protected)
│   ├── components/    # Reusable components
│   │   ├── auth/      # Authentication components
│   │   ├── navigation/# Navigation components
│   │   └── ui/        # UI components
│   ├── lib/           # Utility functions and libraries
│   └── styles/        # Global styles

App Router Structure

The Antimetal boilerplate uses Next.js App Router, which provides a file-system based router built on top of Server Components. The structure is as follows:

  • Route Groups: Folders with names in parentheses like (auth) and (site) are route groups. They don't affect the URL path but help organize the code.
  • Layouts: Each section can have its own layout defined in a layout.tsx file.
  • Pages: Each route has a page.tsx file that defines the main content of the page.
  • Loading States: Optional loading.tsx files define loading UI for the route.
  • Error Handling: Optional error.tsx files define error UI for the route.

Component Architecture

Components are organized into several categories:

  • UI Components: Basic building blocks like buttons, inputs, and cards.
  • Navigation Components: Components related to site navigation, like the navbar and mobile menu.
  • Auth Components: Components related to authentication, like login and registration forms.
  • Feature Components: Components specific to certain features or pages.

Authentication Flow

The authentication system is built with NextAuth.js and follows these principles:

  1. Server-Side Authentication: Authentication state is primarily managed on the server.
  2. Protected Routes: Certain routes (like /dashboard) are protected and require authentication.
  3. Multiple Providers: Support for multiple authentication providers (email/password, OAuth).
  4. Session Management: User sessions are managed securely with cookies.

Styling Approach

The styling approach uses Tailwind CSS with some additional patterns:

  1. Utility-First: Most styling is done with utility classes directly in the JSX.
  2. Component Classes: For complex components, we use the cn() utility to combine classes.
  3. Global Styles: Minimal global styles defined in globals.css.
  4. Responsive Design: Mobile-first approach with responsive breakpoints.

State Management

State management follows these principles:

  1. Local State: React's useState and useReducer for component-level state.
  2. Server State: Server Components for data that can be fetched on the server.
  3. Form State: Form state is managed with controlled components or form libraries.
  4. Global State: Minimal global state, primarily for authentication status.

API Structure

The API follows a RESTful structure:

  1. Route Handlers: API endpoints are defined in the app/api directory.
  2. Authentication: Protected endpoints check for valid sessions.
  3. Error Handling: Consistent error response format.
  4. Data Validation: Input validation for all API requests.

Performance Considerations

The boilerplate is designed with performance in mind:

  1. Server Components: Using React Server Components to reduce client-side JavaScript.
  2. Code Splitting: Automatic code splitting based on routes.
  3. Image Optimization: Using Next.js Image component for optimized images.
  4. Font Optimization: Using Next.js Font system for optimized font loading.
  5. Minimal Dependencies: Keeping external dependencies to a minimum.

Security Considerations

Security is a priority in the boilerplate:

  1. Authentication: Secure authentication with NextAuth.js.
  2. CSRF Protection: Protection against Cross-Site Request Forgery.
  3. Content Security Policy: Strict Content Security Policy headers.
  4. Input Validation: Validation of all user inputs.
  5. Dependency Scanning: Regular scanning for vulnerable dependencies.