Cloudflare Bindings
Cloudflare Workers provide bindings to access platform resources like KV, D1, R2, Durable Objects, AI, and more. vinext applications can access these bindings directly in Server Components, Server Actions, Route Handlers, and middleware.Overview
Bindings are environment variables of special types provided by the Cloudflare Workers runtime. They’re configured inwrangler.jsonc and accessed via the env parameter.
Binding Types
KV (Key-Value Store)
Globally distributed, eventually consistent key-value storage.D1 (SQL Database)
Serverless SQL database built on SQLite.R2 (Object Storage)
S3-compatible object storage.Durable Objects
Stateful serverless objects with strong consistency.AI (Workers AI)
Serverless GPU inference for LLMs and other AI models.Vectorize (Vector Database)
Vector embeddings storage and similarity search.Queues (Message Queue)
Asynchronous message queues.Service Bindings
Call other Workers directly.ASSETS (Static Assets)
Access static files from the Workers Assets binding.IMAGES (Cloudflare Images)
On-the-fly image resizing and transcoding.Accessing Bindings
Worker Entry
Bindings are passed to your worker’sfetch handler:
Server Components
UsegetEnv() to access bindings in Server Components:
Server Actions
Route Handlers
Middleware
Type Safety
Define yourEnv interface once and use it everywhere:
getEnv() Implementation
ThegetEnv() helper uses AsyncLocalStorage to access bindings from anywhere in your app:
Common Patterns
Database Connection Pool
D1 doesn’t require connection pooling (it’s serverless), but you can create a helper:Multi-Tenant KV
Use key prefixes for multi-tenant isolation:Caching Wrapper
Wrap expensive operations with KV caching:Development vs Production
Local Development
Usewrangler dev to access bindings locally:
vinext dev
Thevinext dev command runs Vite’s dev server without Workers bindings. To test with bindings locally:
- Build the app:
vinext build - Run
wrangler dev:wrangler dev
Examples
Full-Stack D1 App
AI-Powered Search
Related
- KV Cache Handler - Use KV for ISR caching
- Image Optimization - Cloudflare Images integration
- Deploying to Cloudflare - Deployment guide
- Cloudflare Workers Docs - Official platform documentation