Skip to content

[A/B Testing] Next.js - provider-agnostic KV adapter (Vercel KV + Upstash Redis) #120

Description

@kuuak

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    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions