App Implementation Guide
Concrete Next.js patterns for Server Actions, Client Providers, and UI separation.
App Implementation Guide (Next.js)
This guide translates WordLoop's overarching Engineering Principles into explicit, copy-pasteable React/Next.js code for the wordloop-app service.
1. Concrete Trace-First Development
Next.js automatically instruments App Router requests with OpenTelemetry. However, when we perform background mutations or explicit fetch requests to the backend proxy, we dynamically enrich the trace.
Identity Propagation (Baggage)
The frontend is responsible for injecting the authenticated user's ID into the W3C Baggage header. This guarantees that all downstream services (Core, ML) can attribute their database queries directly back to the user without fetching identity twice.
2. Concrete Error Handling (Server Actions)
We never throw naked exceptions from Server Actions to Client Components, as this causes hard React Error Boundary crashes. We utilize the Result Pattern to treat errors as standard data.
The Result Pattern
Server Actions return an explicitly typed object containing either the data or the error message, forcing the frontend component to handle failure states gracefully.
Graceful Component Degradation
The React component consumes this pattern directly without needing try/catch blocks.
3. Concrete Dependency Injection (Providers)
Rather than handwriting brittle fetch calls scattered across multiple UI components, we rely entirely on purely generated API clients.
Using the Generated Orval Client
Orval reads our OpenAPI spec and generates pure TypeScript hooks and fetchers. These act as our "Providers" in the Clean Architecture context. Components (the Domain) use them without caring about the underlying HTTP mechanism.
4. Idiomatic React & TypeScript Standards
We do not aim to rewrite foundational guidance on writing excellent React and TypeScript code. Instead, we adhere to established industry baselines mapped to our internal engineering principles.
We expect all Wordloop App engineers to intimately understand:
- Next.js App Router Documentation for framework-dictated rendering boundaries.
- Total TypeScript Patterns for strict TypeScript fundamentals.
Below is concrete guidance on how overarching TS/React idioms manifest as system-enforced architectural invariants.
Default to Server Components (Clean Architecture)
The React Idiom: Start with React Server Components (RSC) and only use 'use client' at the absolute leaf nodes.
The Principle Connection: As defined in our Service Architecture, RSCs act as our "Inbound Adapters." They handle pure data fetching securely on the backend without exposing network waterfalls to the client. This enforces a strict separation where UI interactivity (Client components) is totally decoupled from data orchestration.
Discriminated Unions for Predictable State (Resilience)
The TypeScript Idiom: Using strict discriminated union types instead of optional properties.
The Principle Connection: We avoid try/catch UI crashes by mapping server actions to unified result patterns. Using discriminated unions guarantees the TypeScript compiler will force the frontend engineer to handle both states explicitly, leading to Resilient Error Handling.