|
| 1 | +# LaunchDarkly sample React + Vercel Edge application |
| 2 | + |
| 3 | +We've built a simple web application that demonstrates how the LaunchDarkly React SDK works with |
| 4 | +the Vercel Edge SDK. The app evaluates feature flags using data stored in |
| 5 | +[Vercel Edge Config](https://vercel.com/docs/edge-config/overview) and renders the result using |
| 6 | +React Server Components. |
| 7 | + |
| 8 | +The Vercel SDK reads flag data from Edge Config instead of connecting to LaunchDarkly servers |
| 9 | +directly, providing ultra-low latency flag evaluation at the edge. |
| 10 | + |
| 11 | +The demo shows 2 ways to use React server-side rendering: |
| 12 | + |
| 13 | +1. Using `createLDServerSession` and `useLDServerSession` to provide |
| 14 | +per-request session isolation: Nested Server Components access the session through React's `cache()` |
| 15 | +without any prop drilling. |
| 16 | + |
| 17 | +2. Using the `LDIsomorphicProvider` to bootstrap the browser SDK with server-evaluated flag values. |
| 18 | +This allows the browser SDK to start immediately with real values. |
| 19 | + |
| 20 | +Below, you'll find the build procedure. For more comprehensive instructions, you can visit your |
| 21 | +[Quickstart page](https://app.launchdarkly.com/quickstart#/) or the |
| 22 | +[React SDK reference guide](https://docs.launchdarkly.com/sdk/client-side/react/react-web). |
| 23 | + |
| 24 | +This demo requires Node.js 18 or higher. |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +This example requires the [LaunchDarkly Vercel integration](https://vercel.com/integrations/launchdarkly) |
| 29 | +to be configured. The integration syncs your LaunchDarkly flag data to Vercel Edge Config so that |
| 30 | +the Vercel SDK can read it without connecting to LaunchDarkly servers. |
| 31 | + |
| 32 | +## Build instructions |
| 33 | + |
| 34 | +1. Set the `VERCEL_EDGE_CONFIG` environment variable to your Vercel Edge Config connection string. |
| 35 | + You can find this in your Vercel project settings under Edge Config. |
| 36 | + |
| 37 | + ```bash |
| 38 | + export VERCEL_EDGE_CONFIG="https://edge-config.vercel.com/ecfg_..." |
| 39 | + ``` |
| 40 | + |
| 41 | +2. Set the `LD_CLIENT_SIDE_ID` environment variable to your LaunchDarkly client-side ID. |
| 42 | + The Vercel SDK uses this to look up flag data in Edge Config, and the same value is used |
| 43 | + to bootstrap the browser SDK. |
| 44 | + |
| 45 | + ```bash |
| 46 | + export LD_CLIENT_SIDE_ID="my-client-side-id" |
| 47 | + ``` |
| 48 | + |
| 49 | +3. If there is an existing boolean feature flag in your LaunchDarkly project that you want to |
| 50 | + evaluate, set `LAUNCHDARKLY_FLAG_KEY`: |
| 51 | + |
| 52 | + ```bash |
| 53 | + export LAUNCHDARKLY_FLAG_KEY="my-flag-key" |
| 54 | + ``` |
| 55 | + |
| 56 | + Otherwise, `sample-feature` will be used by default. |
| 57 | + |
| 58 | +## Running |
| 59 | + |
| 60 | +On the command line, run: |
| 61 | + |
| 62 | +```bash |
| 63 | +yarn dev |
| 64 | +``` |
| 65 | + |
| 66 | +Then open [http://localhost:3000](http://localhost:3000) in your browser. You will see the |
| 67 | +spec message, current context name, and a full-page background: green when the flag is on, |
| 68 | +or grey when off. |
| 69 | + |
| 70 | +To simulate a different user, append the `?context=` query parameter. Each tab gets a |
| 71 | +completely independent `LDServerSession` with its own context: |
| 72 | + |
| 73 | +| URL | Context | |
| 74 | +|-----|---------| |
| 75 | +| `http://localhost:3000/` | Sandy (example-user-key) — default | |
| 76 | +| `http://localhost:3000/?context=sandy` | Sandy (example-user-key) | |
| 77 | +| `http://localhost:3000/?context=jamie` | Jamie (user-jamie) | |
| 78 | +| `http://localhost:3000/?context=alex` | Alex (user-alex) | |
| 79 | + |
| 80 | +If you have targeting rules in LaunchDarkly that serve different values to different user keys, |
| 81 | +you will see different flag results for each context. |
| 82 | + |
| 83 | +In a production app, the user identity would come from auth tokens, cookies, or session data |
| 84 | +instead of query parameters. |
0 commit comments