Cloudflare Templates

repository·main·Indexed 24 days ago

https://github.com/cloudflare/templates

A collection of starter templates for building full-stack applications on Cloudflare Workers. Included templates feature Agent Commerce Analytics for recording agent events and trust tiers, AI Agent Visibility for managing agent-discovery surfaces (llms.txt, JSON-LD) with Workers AI enrichment, and AI Brand Visibility for testing brand mentions across multiple models via Cloudflare AI Gateway.

Tokens
76.5K
Snippets
196
Records
395
Agent score
84%

What's inside cloudflare-templates

  1. Overview of the React Router + Hono Full-stack Template

    main

    This template provides a modern full-stack architecture designed to run on Cloudflare Workers. It combines a React-based Single Page Application (SPA) frontend with a Hono-powered backend API. The setup is optimized for edge deployment using the Cloudflare Vite plugin, which handles static asset delivery and seamless local development.

    Key Architecture Components:

    • Frontend: React with React Router for client-side routing, styled with ShadCN UI and Tailwind CSS.
    • Backend: Hono running on Cloudflare Workers, handling API routes (typically under the /api/* path).
    • Build System: Vite with the Cloudflare Vite plugin for unified bundling of frontend and backend assets.
    • Deployment: Managed via Wrangler for worldwide distribution on Cloudflare's edge network.
  2. Overview of OpenAuth Server template

    main

    OpenAuth is a universal provider for managing user authentication. This template demonstrates how to implement login, user registration, and password reset functionality on Cloudflare Workers.

    It utilizes the following Cloudflare services:

    • D1: Used for database storage and state management.
    • KV: Used for authentication storage.
    • Workers Logs: Observability is enabled by default.
  3. Overview of Cloudflare Vertical Microfrontend Router

    main

    The Cloudflare Vertical Microfrontend Router is a production-ready Cloudflare Workers router designed for vertical microfrontend architectures. It acts as a reverse proxy that routes requests to separate Worker services based on path patterns while maintaining the illusion of a single unified application.

    Key capabilities include:

    • Request Routing: Directs traffic to different Workers using configurable path patterns.
    • Mount Prefix Stripping: Removes the prefix (e.g., /docs) before forwarding the request to the upstream service.
    • URL Rewriting: Automatically rewrites HTML/CSS asset URLs, Location headers, and Set-Cookie paths to ensure assets and redirects work correctly within their specific mounts.
    • Performance Optimizations: Supports route preloading via the Speculation Rules API (for Chromium) or a fallback script, and can inject CSS for smooth view transitions.
  4. Overview of Worker Publisher

    main

    Worker Publisher is a Cloudflare Worker designed to programmatically create and deploy other Workers into a Dispatch Namespace using the Cloudflare SDK.

    Key features include:

    • Automatic Namespace Creation: It automatically sets up a Workers for Platforms dispatch namespace.
    • Dynamic Routing: The main Worker acts as a router, forwarding requests to specific deployed Workers via a /{worker-name} path.
    • Isolation: Every deployed Worker runs in its own isolated environment.
    • Extensibility: It can be adapted for deploying static sites, full-stack applications, or personalized AI agents at scale.
  5. Overview of the D1 template functionality

    main

    This template demonstrates how to use a Cloudflare Worker with a D1 binding to execute SQL statements.

    It initializes a comments table with sample data using the following schema logic:

    Query executed by the Worker:

    SELECT * FROM comments LIMIT 3;

    Initial Data Seed:

    INSERT INTO comments (author, content)
    VALUES
        ('Kristian', 'Congrats!'),
        ('Serena', 'Great job!'),
        ('Max', 'Keep up the good work!')
    ;
  6. Project structure of the LLM Chat Application

    main

    The project follows this directory structure:

    • public/: Static assets including index.html (Chat UI) and chat.js (Frontend logic).
    • src/: Backend source code including index.ts (Worker entry point) and types.ts (TypeScript definitions).
    • test/: Test files.
    • wrangler.jsonc: Cloudflare Worker configuration.
    • tsconfig.json: TypeScript configuration.
  7. Understand the SaaS Admin Template features and architecture

    main

    The SaaS Admin Template is a complete dashboard for managing customers and subscriptions.

    Core Capabilities:

    • Admin Dashboard: A modern UI built with Astro and Shadcn UI for managing customer and subscription data.
    • REST API: A built-in API that returns JSON data, secured via API_TOKEN authentication.
    • Customer Workflow: Implements Cloudflare Workflows to perform background tasks for users. This workflow can be triggered through the UI or via the REST API. Refer to customer_workflow.ts in the source for implementation details.

    Tech Stack:

    • Frontend: Astro
    • UI Components: Shadcn UI
    • Database: Cloudflare D1
    • Deployment: Cloudflare Workers
    • Validation: Zod
  8. How the Multiplayer Globe App works

    main

    The Multiplayer Globe App uses Cloudflare Durable Objects to synchronize real-time website visitor locations globally.

    Core Mechanism

    • WebSocket Connections: Each visitor opens a WebSocket connection to a single Durable Object instance that manages the global state.
    • Geolocation: Visitors are placed on the globe using the geographic location of their IP address, which is available via the cf property on the initial HTTP request.
    • State Synchronization: The Durable Object broadcasts new connections (additions to the globe) and connection closures (removals from the globe) to all active visitors.
    • Lifecycle: The Durable Object instance remains active as long as there is at least one open connection. When all connections close, the instance is purged from memory.

    Scaling and Persistence Concepts

    • Scaling: To limit scope (e.g., showing only visitors in the same country), you can create multiple Durable Object instances—one per country—and have clients connect to the instance corresponding to their location.
    • Persistence: This template uses in-memory synchronization. For features requiring data to survive beyond the Durable Object's lifecycle (like an all-time visitor count), you must use the Durable Object Storage API.
  9. How caching works in AI Agent Visibility

    main

    Enrichment (using Workers AI) is performed once per page and the results are cached in KV to avoid expensive repeated calls.

    • TTL: The cache duration is controlled by ENRICHMENT_CACHE_TTL (default: 3600 seconds).
    • Updates: POST /api/resources enriches only the new/changed page and updates the cache in place. POST /api/refresh clears the entire cache, forcing the next read to re-enrich from the source.
    • Resilience: If Workers AI is unavailable, the Worker falls back to deterministic enrichment and caches that degraded result for only 60 seconds to prevent long-term cache poisoning.
  10. How the Durable Chat App works

    main

    The Durable Chat App enables real-time chat between users by assigning each user a unique chat room via a URL ID.

    Key architectural components:

    • Durable Objects: A single Durable Object instance manages each chat room, running in one location to handle all incoming WebSocket connections and ensure synchronization.
    • WebSocket Connections: When a user joins, a WebSocket connection is opened with the Durable Object.
    • Storage: Chat messages are persisted and retrieved using the Durable Object SQL Storage API.
    • Broadcasting: When a message is sent, it is stored in the Durable Object and broadcast to all other connected users in that room.
    • PartyKit: This template utilizes the PartyKit Server API to simplify connection management logic, though it could be implemented using raw Durable Objects.