5️⃣ Port dApp to React
Port dApp to React
The React app is an external consumer of the hosted API. It does not install a private package, import implementation code, or place a credential in browser JavaScript. The app calls same-origin relay paths, validates the public response shape, renders REST state, and uses the SSE stream as a replayable wake-up signal.
The architecture figure makes the browser boundary explicit: only the relay can attach the key, while the React bundle contains public paths and types only. Keep the endpoint provisional until the provider publishes the deployment URL.
The sequence keeps REST authoritative and SSE replayable under the provider's retention/reset contract. A malformed response, wrong MIME, 401/403, disconnect, or rate limit becomes visible state with a bounded retry action instead of an endless reconnect loop. A fatal stream error closes the current EventSource; native reconnect cannot continue, Retry stream preserves the last validated cursor, and Reset cursor and retry clears it for a provider-documented restart at the current tip. Unmount closes the source and cancels that generation.
Scaffold the public consumer
Create the app and install the exact versions used by the tested flow. These commands are consumer setup, not repository-maintainer recipes:
Save the shared cursor module as src/replay-cursor.ts and the checked App source from this page as src/App.tsx. Save the following two files at the app root; the relay owns the provider key and forwards only the public response diagnostics:
The relay fixture injects the key in the server process and forwards only public diagnostics and content type. It sets local REST no-store and SSE no-cache, drops cookies, redirects, authentication challenges, CSP, upstream cache, and body-framing headers, and leaves the browser unable to see the provider credential. It enforces the same origin-root NEXUS_API_URL contract as the raw and full-stack examples; a provider base path must be handled by a separate adapter that preserves it.
Replace App.tsx
Replace src/App.tsx with the checked source in react-app/App.tsx:
The source block and the repository fixture are byte-aligned by the React-flow gate. The code validates list and event shapes, uses text interpolation only for React text nodes, closes the EventSource on malformed data, and exposes a visible REST retry action. It does not create an API-key header in browser code because the relay owns that header.
Run the consumer
Keep the key in the relay's server-side environment, never in a VITE-prefixed variable. Run the public app with the same commands a consumer can use:
Maintainers may separately run the repository's relay, React-flow, audit, and secret-regression gates against temporary copies. Those maintenance commands are not prerequisites for an external consumer. A provider outage or a provisional URL is a service boundary, not a reason to weaken the credential rule.
Production boundary
Use a reviewed server relay or backend service for production. Add application authentication, authorization, CSRF protection, observability, and a deployment-specific allowlist while preserving upstream status/body and the public request-id, retry, and rate-limit diagnostics. Never expose the key to a browser bundle, URL, query string, local storage, or source control.
Next
Return to Connect a Nexus API dApp for relay patterns, or consult the HTTP API reference for endpoint and status contracts.
Last updated

