Cloudflare Pages Functions (API)
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Cloudflare Pages Functions provide the server-side compute layer for the Lou Gehrig Fan Club (LGFC) platform, enabling dynamic backend functionality within a static-first Next.js architecture. These functions act as API endpoints that interface with Cloudflare D1 for data persistence, handling tasks such as content management, member requests, and media metadata retrieval.
The system leverages a "pass-through" routing strategy for static assets while intercepting specific API paths to execute business logic. This ensures that the application remains "compile-safe" for Cloudflare Pages, maintaining functionality even if specific API routes are temporarily unavailable or return 404 errors during build-time static generation.
Sources: context.md:14-15, src/app/admin/content/page.tsx:8-12, functions/[[path]].ts:1-5
Routing Architecture
The routing logic distinguishes between static assets (Next.js internals, images, icons) and dynamic requests. A catch-all function handles path resolution to ensure correct mapping to static .html files or fallback to the static asset layer.
Asset vs. Function Resolution
The system explicitly avoids intercepting requests for /_next/, /images/, /favicon.ico, and other static files. For clean URLs (e.g., /about), the function attempts to resolve index.html or .html snapshots before handing off to the environment's static content fetcher.
The diagram shows the logic used to resolve path requests to static assets in a Cloudflare Pages environment.
Sources: functions/[[path]].ts:14-65
Database Integration (Cloudflare D1)
API functions interact with Cloudflare D1, a relational database, to manage project content and member data. Common operations include preparing SQL statements with bound parameters to prevent injection and returning standardized JSON responses.
Content Management API
The content system uses specific endpoints to manage site sections. These endpoints support a draft/publish workflow where content is stored as raw text (Markdown) within the content_blocks table.
| Endpoint | Method | Description |
|---|---|---|
/api/admin/content | GET | Fetches live and draft section bundles for a specific slug. |
/api/admin/content/draft | POST | Saves a content draft for a section, including optional asset URLs. |
/api/admin/content/publish | POST | Promotes a draft section to the live state. |
Sources: src/app/admin/content/page.tsx:47-133, src/app/admin/cms/page.tsx:14-19
Media and Library APIs
Specific functions handle specialized data types like the library entries and photo archives.
The sequence diagram illustrates the typical data flow for fetching photo metadata from the D1 database via an API function.
Sources: lgfc-lite-configure-remaining.sh:185-217
System Diagnostics and Maintenance
The project includes diagnostic endpoints and scripts to monitor the health of the API layer and database bindings.
Diagnostic Endpoints
The /api/d1-test endpoint serves as an admin-only diagnostic tool. It performs the following checks:
- D1 Binding: Verifies that the
DBenvironment variable is correctly bound to the function context. - Schema Validation: Checks for the existence of required tables (e.g.,
members,photos,join_requests). - Metadata Inspection: Allows admins to query column information for specific tables using
PRAGMA table_info.
Sources: functions/api/d1-test.ts:16-110
Build and Deployment Scripts
Configuration scripts are used to provision these functions and ensure consistent environment settings.
wrangler pages dev: Used for local development, binding D1 and enabling nodejs compatibility flags.lgfc-lite-configure-remaining.sh: An idempotent script that creates or overwrites API function files (e.g.,/api/library/list,/api/photos/get) to match the latest schema requirements.
Sources: package.json:54, lgfc-lite-configure-remaining.sh:1-12
Data Models
The API functions expect and return specific data structures defined within the project's TypeScript types.
| Data Type | Fields | Source |
|---|---|---|
| ContentBlock | slug, section, content, asset_url, updated_at | src/app/admin/content/page.tsx:14-20 |
| PhotoItem | id, url, is_memorabilia, description, created_at | lgfc-lite-configure-remaining.sh:189 |
| LibraryListItem | id, title, content, created_at | lgfc-lite-configure-remaining.sh:41-46 |
Conclusion
The Cloudflare Pages Functions in the LGFC project provide a robust API layer that bridges the gap between a static Next.js frontend and a dynamic D1 database. By employing structured routing, administrative diagnostic tools, and idempotent configuration scripts, the architecture ensures high availability and technical consistency across production and development environments.