|
| 1 | +# LaunchDarkly sample React server-side application |
| 2 | + |
| 3 | +We've built a simple web application that demonstrates how the LaunchDarkly React SDK works with |
| 4 | +React Server Components (RSC). The app evaluates a feature flag on the server and renders the |
| 5 | +result — no client-side JavaScript required. |
| 6 | + |
| 7 | +The demo also shows how `createLDServerSession` and `useLDServerSession` work together to provide |
| 8 | +per-request session isolation: every HTTP request creates its own `LDServerSession` bound to |
| 9 | +that request's user context. Nested Server Components access the session through React's `cache()` |
| 10 | +without any prop drilling. |
| 11 | + |
| 12 | +Below, you'll find the build procedure. For more comprehensive instructions, you can visit your |
| 13 | +[Quickstart page](https://app.launchdarkly.com/quickstart#/) or the |
| 14 | +[React SDK reference guide](https://docs.launchdarkly.com/sdk/client-side/react/react-web). |
| 15 | + |
| 16 | +This demo requires Node.js 18 or higher. |
| 17 | + |
| 18 | +## How it works |
| 19 | + |
| 20 | +| Module | Role | |
| 21 | +|--------|------| |
| 22 | +| `ldBaseClient` (module-level) | A singleton Node SDK client, initialized once per process. Shared across all requests. | |
| 23 | +| `createLDServerSession(ldBaseClient, context)` | Called once per request in `app/page.tsx`. Binds the request context to the client and stores the session in React's `cache()`. | |
| 24 | +| `useLDServerSession()` (in `FeatureSection.tsx`) | Retrieves the session from React's per-request cache. No props needed — React isolates each request automatically. | |
| 25 | + |
| 26 | +To observe per-request isolation, open browser tabs with different `context` query parameters. |
| 27 | +Each tab gets a completely independent `LDServerSession` with its own context: |
| 28 | + |
| 29 | +``` |
| 30 | +http://localhost:3000/?context=sandy |
| 31 | +http://localhost:3000/?context=jamie |
| 32 | +http://localhost:3000/?context=alex |
| 33 | +``` |
| 34 | + |
| 35 | +In a production app, the user identity would come from auth tokens, cookies, or session data |
| 36 | +instead of query parameters. |
| 37 | + |
| 38 | +## Build instructions |
| 39 | + |
| 40 | +1. Set the value of the `LAUNCHDARKLY_SDK_KEY` environment variable to your LaunchDarkly SDK key. |
| 41 | + |
| 42 | + ```bash |
| 43 | + export LAUNCHDARKLY_SDK_KEY="my-sdk-key" |
| 44 | + ``` |
| 45 | + |
| 46 | +2. If there is an existing boolean feature flag in your LaunchDarkly project that you want to |
| 47 | + evaluate, set `LAUNCHDARKLY_FLAG_KEY`: |
| 48 | + |
| 49 | + ```bash |
| 50 | + export LAUNCHDARKLY_FLAG_KEY="my-flag-key" |
| 51 | + ``` |
| 52 | + |
| 53 | + Otherwise, `sample-feature` will be used by default. |
| 54 | + |
| 55 | +3. On the command line, run: |
| 56 | + |
| 57 | + ```bash |
| 58 | + yarn dev |
| 59 | + ``` |
| 60 | + |
| 61 | + Then open [http://localhost:3000](http://localhost:3000) in your browser. You will see the |
| 62 | + spec message, current context name, and a full-page background: green when the |
| 63 | + flag is on, or grey when off. |
| 64 | + |
| 65 | +4. To simulate a different user, append the `?context=` query parameter: |
| 66 | + |
| 67 | + | URL | Context | |
| 68 | + |-----|---------| |
| 69 | + | `http://localhost:3000/` | Sandy (example-user-key) — default | |
| 70 | + | `http://localhost:3000/?context=sandy` | Sandy (example-user-key) | |
| 71 | + | `http://localhost:3000/?context=jamie` | Jamie (user-jamie) | |
| 72 | + | `http://localhost:3000/?context=alex` | Alex (user-alex) | |
| 73 | + |
| 74 | + If you have targeting rules in LaunchDarkly that serve different values to different user keys, |
| 75 | + you will see different flag results for each context. |
0 commit comments