Cloudflare Functions
Relevant source files
The following files were used as context for generating this wiki page:
Cloudflare Functions serve as the server-side execution layer for the Bolt application, leveraging Cloudflare Pages and Workers to handle request routing and backend logic. The project is built using the Remix framework, which is integrated with Cloudflare's infrastructure to provide a full-stack environment.
Sources: CONTRIBUTING.md:46-48, package.json:28-29
Infrastructure and Deployment
The server-side logic is deployed as a Cloudflare Pages application. The configuration for this environment is managed through wrangler.toml and processed during the build step via Vite.
Deployment Configuration
The project uses wrangler for managing Cloudflare deployment and local development emulation. The environment is configured for nodejs_compat to support Node.js APIs within the worker environment.
| Property | Value | Description |
|---|---|---|
name | "bolt" | The name of the Cloudflare project. |
compatibility_flags | ["nodejs_compat"] | Enables Node.js API support in the worker. |
compatibility_date | "2024-07-01" | Standardizes runtime behavior. |
pages_build_output_dir | "./build/client" | Directory for static assets. |
Sources: wrangler.toml:1-5
Build and Execution Flow
The application uses a specialized handler to bridge Remix server builds with Cloudflare Pages. The functions/[[path]].ts file acts as a catch-all route for all server requests.
This diagram illustrates how incoming requests are captured by the Cloudflare Pages function and passed to the Remix runtime.
Sources: functions/[[path]].ts:1-8
Server-Side Request Handling
The server-side logic handles complex tasks such as streaming AI responses and prompt enhancement. These are implemented as Remix Actions that run within the Cloudflare environment.
Prompt Enhancement API
One specific function provided by the backend is the api.enhancer route. This endpoint takes a user prompt and uses an LLM to improve it, returning a streamed text response.
- Endpoint:
/api/enhancer - Method:
POST - Logic:
- Receives a JSON payload containing a
message. - Invokes
streamTextusing the Cloudflare environment bindings (context.cloudflare.env). - Transforms the LLM stream into an AI-compatible stream format.
- Returns a
StreamingTextResponse.
- Receives a JSON payload containing a
Sources: app/routes/api.enhancer.ts:12-62
Development and Local Emulation
The project provides scripts to replicate the Cloudflare environment locally using wrangler pages dev.
# Script from package.json
"start": "bindings=$(./bindings.sh) && wrangler pages dev ./build/client $bindings"
Sources: package.json:16, CONTRIBUTING.md:73-75
Dependency Integration
The Cloudflare Functions environment depends on specific packages for both the Remix integration and the AI capabilities.
| Dependency | Purpose |
|---|---|
@remix-run/cloudflare | Core Remix utilities for Cloudflare. |
@remix-run/cloudflare-pages | Adapter for Cloudflare Pages deployment. |
@cloudflare/workers-types | TypeScript definitions for the Worker environment. |
ai | Vercel AI SDK for streaming responses. |
Sources: package.json:44-46, package.json:84
Vite Integration
The build process uses remixCloudflareDevProxy to provide a bridge between the Vite development server and the Cloudflare worker environment during local development.
Sources: vite.config.ts:1-18
Cloudflare Functions provide the essential backbone for Bolt's server-side operations, enabling high-performance streaming AI features and full-stack application logic within the Cloudflare ecosystem.