Skip to main content

App Router Architecture

Horse Trust uses Next.js 16’s App Router, which provides file-system based routing with enhanced features like layouts, server components, and streaming.

Route Structure

Root Layout

The root layout wraps all pages and provides global UI elements. Location: app/layout.tsx
app/layout.tsx

Key Features:

  • Server Component: Runs on the server for optimal performance
  • Cookie Access: Reads authentication state from cookies
  • Metadata Export: Sets page title and description for SEO
  • Global Components: Header and Footer included in all pages

Public Routes

Homepage

Route: / File: app/page.tsx
app/page.tsx

Marketplace Listing

Route: /marketplace File: app/marketplace/page.tsx
app/marketplace/page.tsx
Features:
  • Server-side data fetching
  • Dynamic rendering with force-dynamic
  • Revalidation every 60 seconds
  • Error handling for API failures

Dynamic Routes

Horse Detail Page

Route: /marketplace/[id] File: app/marketplace/[id]/page.tsx URL Example: /marketplace/123 Dynamic routes use folder names with square brackets to create parameterized URLs:
Dynamic Segment Access:
  • params.id contains the dynamic segment value
  • Type-safe with TypeScript
  • Can be used for data fetching

Authentication Routes

Login Page

Route: /login File: app/login/page.tsx Type: Client Component
app/login/page.tsx

Registration Page

Route: /registro File: app/registro/page.tsx Purpose: New user registration

Protected Routes

Dashboard

Route: /dashboard File: app/dashboard/page.tsx Access: Requires authentication
app/dashboard/page.tsx
Protection Strategy:
  1. Read authentication cookie
  2. If cookie missing, redirect to /login
  3. Validate token with API
  4. If invalid, redirect to /login
  5. Render protected content

Server Actions

Server Actions provide secure server-side mutations from client components. Location: app/actions/auth.ts
app/actions/auth.ts
Usage in Client Component:
Use Next.js Link for client-side navigation:

Programmatic Navigation

Use useRouter in client components:

Server-Side Redirects

Use redirect in server components:

Route Configuration

Dynamic Rendering

Force dynamic rendering for pages that need real-time data:

Revalidation

Set revalidation time for data fetching:

No Cache

Disable caching for sensitive data:

Path Aliases

TypeScript path aliases configured in tsconfig.json:
Usage:

Middleware (Future Enhancement)

Middleware can be added at middleware.ts for route protection:
middleware.ts

Route Groups (Optional Pattern)

Route groups can organize routes without affecting URL structure:
This creates shared layouts without adding route segments.