Timeout and upstream failure
What you need to know
A BFF needs a deadline so an unavailable dependency cannot make every request wait indefinitely. Separate upstream timeout, upstream rejection and a successful response with invalid data.
By the end, you can: choose timeout budget; map failure to response.
Bound an upstream read
// Partial BFF helper: approved fixed lab origin and small synthetic response.export async function upstream(signal: AbortSignal) { const deadline = AbortSignal.timeout(800); const combined = AbortSignal.any([signal, deadline]); try { const response = await fetch('http://127.0.0.1:4317/catalog', { signal: combined, cache: 'no-store', redirect: 'error' }); if (!response.ok) return { status: 502, code: 'UPSTREAM_STATUS' }; let body: unknown; try { body = await response.json(); } // lab-only bounded origin catch (error) { if (error instanceof SyntaxError) return { status: 502, code: 'UPSTREAM_DATA' }; throw error; // body abort or transport failure is handled below } if (typeof body !== 'object' || body === null || !('revision' in body) || typeof body.revision !== 'number' || !Number.isSafeInteger(body.revision) || body.revision < 1) return { status: 502, code: 'UPSTREAM_DATA' }; return { status: 200, revision: body.revision }; } catch (error) { if (signal.aborted) throw error; if (deadline.aborted) return { status: 504, code: 'UPSTREAM_TIMEOUT' }; return { status: 502, code: 'UPSTREAM_FAILED' }; }}Expected behavior (not framework-tested): With a cooperative fetch, the local deadline limits waiting. Caller cancellation is distinguished from timeout; the caller owns its cancellation handling.
How it works
Use a bounded time budget appropriate to the feature. An AbortSignal can be passed to fetch to stop waiting for a request or body read; combine request cancellation with the deadline where the runtime supports AbortSignal.any. This is cooperative: it does not guarantee a remote service stops processing a request. A timed-out mutation may have succeeded remotely, so avoid automatic retries of non-idempotent operations. For mandatory read dependencies, map a known deadline failure to 504, an upstream failure or malformed upstream response to a suitable 502, and local unexpected errors to 500. These are application choices, not automatic fetch mappings.
A successful fetch promise does not imply a successful status; check response.ok. Avoid arbitrary caller-provided upstream URLs. Use a fixed approved origin, controlled paths, no credentials in error messages and a separate response size bound. The lab uses a small local synthetic origin; the production boundary requires allowlisting and operational safeguards.
Map stable errors without leaking details
export function unavailable(timedOut: boolean) { return Response.json({ error: timedOut ? 'UPSTREAM_TIMEOUT' : 'UPSTREAM_FAILED' }, { status: timedOut ? 504 : 502, headers: { 'Cache-Control': 'no-store' } });}Expected behavior (not framework-tested): A timeout returns 504; other modeled upstream failures return 502. Neither includes internal host names or stack traces.
Common mistake
Do not catch every exception and return a fake successful empty result. Also avoid retrying a mutation after a timeout without an idempotency contract: the remote write may have completed before the response was lost.
Practice and verification
Choose a budget for a mandatory catalog read and an optional recommendation read. For each, define what the UI can safely show on timeout. Exercise a controlled failed dependency and record whether cancellation and cleanup actually occurred, rather than relying on a sleep alone.
Scope: App Router, proposed Next.js 16.x, Cache Components disabled for these labs. Snippets assume a reviewed TypeScript app and are static examples; framework behavior has not been executed. Exact supported patch and dependency set must be verified before running.