Duffer Derek

Current Path : /var/www/candid-dev-nxt.wpress.dk/httpdocs/
Upload File :
Current File : /var/www/candid-dev-nxt.wpress.dk/httpdocs/README.md

# Candid Next.js Application

A headless WordPress + Next.js application for Candid.org, featuring Incremental Static Regeneration (ISR) and real-time content updates from WordPress CMS.

## πŸ“‹ Table of Contents

- [Overview](#overview)
- [Architecture](#architecture)
- [Tech Stack](#tech-stack)
- [Project Structure](#project-structure)
- [Getting Started](#getting-started)
- [Environment Variables](#environment-variables)
- [Development](#development)
- [Build & Deployment](#build--deployment)
- [Key Features](#key-features)
- [Documentation](#documentation)

## 🎯 Overview

This is a headless CMS setup where:
- **WordPress** (hosted on WP Engine) serves as the content management system
- **Next.js** (hosted on WP Engine) serves as the frontend application
- Content is fetched via **GraphQL** (WPGraphQL) and **REST API** endpoints
- Pages are statically generated with **ISR (Incremental Static Regeneration)**
- Content updates trigger automatic revalidation of affected pages

## πŸ—οΈ Architecture

```
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   WordPress     │────────▢│   GraphQL/REST   │────────▢│   Next.js    β”‚
β”‚   (WP Engine)   β”‚         β”‚      APIs        β”‚         β”‚  (WP Engine) β”‚
β”‚                 β”‚         β”‚                  β”‚         β”‚              β”‚
β”‚  Content Edit   β”‚         β”‚  Data Fetching   β”‚         β”‚  Static Pagesβ”‚
β”‚  Save/Update    │────────▢│  Revalidation    │────────▢│  ISR Cache   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

### Data Flow

1. **Content Creation**: Editor creates/updates content in WordPress
2. **Webhook Trigger**: WordPress hooks (`save_post`, `post_updated`) trigger revalidation
3. **Cache Purge**: WP Engine cache layers are cleared (Varnish, Memcached, CDN)
4. **Revalidation**: Next.js `/api/revalidate` endpoint is called
5. **ISR Update**: Next.js regenerates the affected page in the background
6. **Serving**: Updated page is served to users

## πŸ’» Tech Stack

### Frontend
- **Next.js 14.2.26** - React framework with Pages Router
- **React 18** - UI library
- **GraphQL Request** - GraphQL client
- **SCSS/SASS** - Styling
- **Material-UI** - Component library
- **React Query** - Data fetching and caching

### Backend
- **WordPress** - Headless CMS
- **WPGraphQL** - GraphQL API
- **ACF (Advanced Custom Fields)** - Custom field management
- **Custom REST API** - Additional endpoints

### Hosting
- **WP Engine** - Hosting for both WordPress and Next.js
- **Multiple Cache Layers**: Varnish, Memcached, CDN

## πŸ“ Project Structure

```
candid-nextjs-app-v2/
β”œβ”€β”€ api/                    # API fetchers and queries
β”‚   β”œβ”€β”€ pages/             # Page data fetching
β”‚   β”œβ”€β”€ blogs/             # Blog post fetching
β”‚   β”œβ”€β”€ press/             # Press release fetching
β”‚   β”œβ”€β”€ people/            # People/team data
β”‚   β”œβ”€β”€ settings/          # Global settings
β”‚   └── misc/              # Miscellaneous data
β”œβ”€β”€ components/            # React components
β”‚   β”œβ”€β”€ common/           # Shared components
β”‚   β”œβ”€β”€ forms/            # Form components
β”‚   β”œβ”€β”€ hero/             # Hero sections
β”‚   └── ...
β”œβ”€β”€ pages/                 # Next.js pages (routes)
β”‚   β”œβ”€β”€ [...pageSlug]/    # Dynamic page routes
β”‚   β”œβ”€β”€ api/              # API routes
β”‚   β”œβ”€β”€ blogs/            # Blog pages
β”‚   └── press/            # Press pages
β”œβ”€β”€ pageTemplates/         # Page template components
β”œβ”€β”€ templateBlocks/        # Reusable content blocks
β”œβ”€β”€ wp-theme/             # WordPress theme (headless)
β”‚   └── candid/           # Custom theme files
β”œβ”€β”€ constants/            # Configuration constants
β”œβ”€β”€ helpers/             # Utility functions
β”œβ”€β”€ hooks/               # Custom React hooks
└── public/              # Static assets
```

See [Documentation](#documentation) section for detailed directory documentation.

## πŸš€ Getting Started

### Prerequisites

- Node.js 18+ and npm
- Access to WordPress backend (for API endpoints)
- Environment variables configured (see [Environment Variables](#environment-variables))

### Installation

```bash
# Install dependencies
npm install

# Run development server
npm run dev

# Build for production
npm run build

# Start production server
npm start
```

The application will be available at `http://localhost:3000` (or port 3018 for production mode).

## πŸ” Environment Variables

Create a `.env.local` file in the root directory with the following variables:

```env
# WordPress Backend URLs
BASE_URL=https://cms.candid.org - Live
BASE_URL_DEV=https://cms-dev.candid.org - Dev

# Next.js Frontend URLs
NXT_URL=https://candid.org (https://dev.candid.org - Dev)
NXT_DOMAIN=candid.org

# GraphQL Endpoint
API_URL=https://cms.candid.org/graphql

# Revalidation Secret (must match WordPress theme)
REVALIDATE_SECRET=your-secret-key-here

# Environment
CURRENT_ENV=development  # or 'production'
```

**Note**: The `constants/general.js` file contains default values but should be overridden with environment variables in production.

## πŸ’» Development

### Available Scripts

- `npm run dev` - Start development server with hot reload
- `npm run build` - Build production bundle
- `npm start` - Start production server (port 3018)
- `npm run lint` - Run ESLint

### Development Workflow

1. **Local Development**: Run `npm run dev` and connect to staging WordPress backend
2. **Testing**: Test against staging WordPress API endpoints
3. **Build Testing**: Run `npm run build` to test production build locally
4. **Deployment**: Push to repository (WP Engine handles deployment)

### Code Style

- Use functional components with hooks
- Follow React best practices
- Use SCSS for styling (component-scoped files)
- Follow existing naming conventions

## πŸ—οΈ Build & Deployment

### Build Process

1. **Static Generation**: Next.js pre-renders pages at build time
2. **ISR Setup**: Pages are configured with `revalidate` intervals
3. **API Routes**: Serverless functions are created for `/api/*` routes
4. **Asset Optimization**: Images, fonts, and CSS are optimized

### Deployment

- **Hosting**: WP Engine (managed hosting)
- **Build Command**: `npm run build`
- **Start Command**: `npm start`
- **Port**: 3018 (production)

### ISR Revalidation

Pages are automatically revalidated when:
- WordPress content is updated (via webhook)
- Revalidation interval expires (configured per page)
- Manual revalidation is triggered via `/api/revalidate`

## ✨ Key Features

### 1. Incremental Static Regeneration (ISR)
- Pages are statically generated at build time
- Background revalidation keeps content fresh
- Configurable revalidation intervals per page

### 2. Dynamic Routing
- Catch-all routes for WordPress pages: `[...pageSlug]`
- Dynamic routes for blogs, press, people, authors
- Server-side rendering for search and dynamic content

### 3. Content Management
- Real-time content updates from WordPress
- Automatic cache invalidation
- WP Engine cache layer integration

### 4. Performance
- Static page generation for fast loads
- Image optimization via Next.js Image component
- Code splitting and lazy loading
- CDN caching via WP Engine

## πŸ“š Documentation

Detailed documentation is available in the following directories:

- [`/api/README.md`](./api/README.md) - API structure and data fetching
- [`/components/README.md`](./components/README.md) - Component architecture
- [`/pages/README.md`](./pages/README.md) - Page structure and routing
- [`/wp-theme/README.md`](./wp-theme/README.md) - WordPress theme documentation
- [`/constants/README.md`](./constants/README.md) - Configuration constants
- [`/helpers/README.md`](./helpers/README.md) - Utility functions

## πŸ› Troubleshooting

### Common Issues

1. **White Screen of Death**
   - Check browser console for errors
   - Verify API endpoints are accessible
   - Check revalidation logs in WordPress
   - See [Troubleshooting Guide](./docs/TROUBLESHOOTING.md) (if available)

2. **Build Failures**
   - Ensure all environment variables are set
   - Check WordPress API connectivity
   - Verify GraphQL queries are valid

3. **Revalidation Not Working**
   - Verify `REVALIDATE_SECRET` matches in both WordPress and Next.js
   - Check WordPress error logs
   - Verify network connectivity between WordPress and Next.js

## 🀝 Contributing

1. Create a feature branch
2. Make your changes
3. Test thoroughly (local and staging)
4. Submit a pull request

## πŸ“ Notes

- **WP Engine Specific**: This setup is optimized for WP Engine hosting
- **Cache Layers**: Multiple cache layers (Varnish, Memcached, CDN) require careful cache invalidation
- **Revalidation**: Automatic revalidation depends on WordPress hooks and network connectivity

## πŸ”— Related Resources

- [Next.js Documentation](https://nextjs.org/docs)
- [WPGraphQL Documentation](https://www.wpgraphql.com/)
- [WP Engine Documentation](https://wpengine.com/support/)

---

**Last Updated**: 2026-01-28

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists