Context
Part of # A/B Testing for Superstack.111
What to build
A provider-agnostic KV adapter for reading/writing the A/B experiment registry from edge middleware. Must support Vercel KV (Vercel deployments) and Upstash Redis (self-hosted deployments).
Interface
// next/src/lib/kv/types.ts
interface KVAdapter {
get<T>(key: string): Promise<T | null>;
set(key: string, value: unknown, ttl?: number): Promise<void>;
del(key: string): Promise<void>;
}
Implementations
src/lib/kv/adapters/vercel-kv. uses @vercel/kvts
src/lib/kv/adapters/upstash. uses @upstash/redis (REST API, edge-compatible)ts
Factory
src/lib/kv/index.ts selects the adapter based on NEXT_PUBLIC_KV_PROVIDER=vercel|upstash env var, with auto-detection fallback.
Environment variables
| Variable |
Required by |
NEXT_PUBLIC_KV_PROVIDER |
Factory (adapter selection) |
UPSTASH_REDIS_REST_URL |
Upstash adapter |
UPSTASH_REDIS_REST_TOKEN |
Upstash adapter |
| Vercel KV vars |
Set automatically by Vercel dashboard |
Edge runtime requirement
Both adapters must be compatible with the Next.js Edge runtime (no Node.js APIs).
Files to create
next/src/lib/kv/
types.ts
index.ts # Adapter factory
adapters/
vercel-kv.ts
upstash.ts
Context
Part of # A/B Testing for Superstack.111
What to build
A provider-agnostic KV adapter for reading/writing the A/B experiment registry from edge middleware. Must support Vercel KV (Vercel deployments) and Upstash Redis (self-hosted deployments).
Interface
Implementations
src/lib/kv/adapters/vercel-kv. uses@vercel/kvtssrc/lib/kv/adapters/upstash. uses@upstash/redis(REST API, edge-compatible)tsFactory
src/lib/kv/index.tsselects the adapter based onNEXT_PUBLIC_KV_PROVIDER=vercel|upstashenv var, with auto-detection fallback.Environment variables
NEXT_PUBLIC_KV_PROVIDERUPSTASH_REDIS_REST_URLUPSTASH_REDIS_REST_TOKENEdge runtime requirement
Both adapters must be compatible with the Next.js Edge runtime (no Node.js APIs).
Files to create