|
| 1 | +# @replanejs/next Example |
| 2 | + |
| 3 | +A Next.js App Router example demonstrating how to use `@replanejs/next` for server-side rendered feature flags with real-time updates. |
| 4 | + |
| 5 | +## Setup |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install |
| 9 | +``` |
| 10 | + |
| 11 | +## Running |
| 12 | + |
| 13 | +Create a `.env.local` file with your Replane credentials: |
| 14 | + |
| 15 | +```env |
| 16 | +# Server-side (not exposed to client) |
| 17 | +REPLANE_BASE_URL=https://replane.example.com |
| 18 | +REPLANE_SDK_KEY=your-sdk-key |
| 19 | +
|
| 20 | +# Client-side (exposed via NEXT_PUBLIC_ prefix) |
| 21 | +NEXT_PUBLIC_REPLANE_BASE_URL=https://replane.example.com |
| 22 | +NEXT_PUBLIC_REPLANE_SDK_KEY=your-sdk-key |
| 23 | +``` |
| 24 | + |
| 25 | +Then start the development server: |
| 26 | + |
| 27 | +```bash |
| 28 | +npm run dev |
| 29 | +``` |
| 30 | + |
| 31 | +## What this example demonstrates |
| 32 | + |
| 33 | +- Server-side config fetching with `getReplaneSnapshot()` |
| 34 | +- Zero-loading-state hydration with `ReplaneNextProvider` |
| 35 | +- `useConfig` hook for reactive client components |
| 36 | +- Real-time updates via SSE after hydration |
| 37 | +- Type-safe configuration with TypeScript |
| 38 | +- Context-based override evaluation |
| 39 | + |
| 40 | +## Architecture |
| 41 | + |
| 42 | +``` |
| 43 | +┌─────────────────────────────────────────────────────────────┐ |
| 44 | +│ Server (RSC) │ |
| 45 | +│ ┌───────────────────────────────────────────────────────┐ │ |
| 46 | +│ │ getReplaneSnapshot() │ │ |
| 47 | +│ │ - Fetches configs from Replane API │ │ |
| 48 | +│ │ - Returns serializable snapshot │ │ |
| 49 | +│ └───────────────────────────────────────────────────────┘ │ |
| 50 | +└─────────────────────────────────────────────────────────────┘ |
| 51 | + │ |
| 52 | + ▼ snapshot (serialized) |
| 53 | +┌─────────────────────────────────────────────────────────────┐ |
| 54 | +│ Client │ |
| 55 | +│ ┌───────────────────────────────────────────────────────┐ │ |
| 56 | +│ │ ReplaneNextProvider │ │ |
| 57 | +│ │ - Restores client from snapshot (instant) │ │ |
| 58 | +│ │ - Connects to Replane for real-time updates │ │ |
| 59 | +│ └───────────────────────────────────────────────────────┘ │ |
| 60 | +│ │ │ |
| 61 | +│ ▼ │ |
| 62 | +│ ┌───────────────────────────────────────────────────────┐ │ |
| 63 | +│ │ useConfig() / useReplane() │ │ |
| 64 | +│ │ - Access configs in any client component │ │ |
| 65 | +│ │ - Auto re-render on config changes │ │ |
| 66 | +│ └───────────────────────────────────────────────────────┘ │ |
| 67 | +└─────────────────────────────────────────────────────────────┘ |
| 68 | +``` |
0 commit comments