Skip to main content

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 in wrangler.jsonc and accessed via the env parameter.

Binding Types

KV (Key-Value Store)

Globally distributed, eventually consistent key-value storage.
See KV Cache Handler for ISR integration.

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.
The ASSETS binding is used internally by vinext for static file serving and image optimization. You typically don’t access it directly.

IMAGES (Cloudflare Images)

On-the-fly image resizing and transcoding.
See Image Optimization for usage.

Accessing Bindings

Worker Entry

Bindings are passed to your worker’s fetch handler:

Server Components

Use getEnv() to access bindings in Server Components:

Server Actions

Route Handlers

Middleware

Type Safety

Define your Env interface once and use it everywhere:
Now TypeScript enforces correct binding types throughout your app.

getEnv() Implementation

The getEnv() helper uses AsyncLocalStorage to access bindings from anywhere in your app:
If called outside a request context, it throws an error.

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

Use wrangler dev to access bindings locally:
This starts a local Workers runtime with your configured bindings. D1 uses SQLite locally, KV uses in-memory storage.

vinext dev

The vinext dev command runs Vite’s dev server without Workers bindings. To test with bindings locally:
  1. Build the app: vinext build
  2. Run wrangler dev: wrangler dev
Or use the vinext deploy preview:

Examples

Full-Stack D1 App