Next.js BFF (Backend for Frontend)
Implement a BFF using Route Handlers with explicit validation and caching.
When to Use
- •Aggregating multiple backend APIs
- •Hiding credentials and tokens
- •Normalizing responses for the UI
- •Enforcing per-tenant isolation (if multi-tenant)
Inputs
- •External services to aggregate
- •Auth requirements
- •Cache strategy (no-store vs revalidate)
- •Tenant identification (subdomain, path, header)
Instructions
- •Create Route Handlers under
app/api/. - •Fetch from upstream services on the server with timeouts.
- •Normalize and validate responses.
- •Apply caching and revalidation explicitly.
- •Map upstream errors to stable API responses.
- •Enforce tenant isolation in queries and caches.
Output
- •BFF endpoints with typed, normalized responses and cache strategy.