|
1 | | -# Complete example app for Vercel LaunchDarkly SDK |
| 1 | +# LaunchDarkly sample Vercel application |
2 | 2 |
|
3 | | -This example shows how to evaluate feature flags in Vercel's edge runtime using the [LaunchDarkly Vercel SDK](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/vercel). Two primary use cases are highlighted: |
| 3 | +This example shows how to evaluate feature flags in Vercel's edge runtime using the |
| 4 | +[LaunchDarkly Vercel SDK](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/vercel). |
| 5 | +Two primary use cases are highlighted: |
4 | 6 |
|
5 | | -1. Bootstrapping feature flags from the edge runtime and consuming them in the [LaunchDarkly Client-side SDK for React](https://github.com/launchdarkly/react-client-sdk). This is leveraging feature flags in edge-rendered pages while still maintaining the events and ergonomics provided by the React SDK. You can see details in [`app/layout.tsx`](./app/layout.tsx) and [`components/launchdarklyProvider.tsx`](./components/launchdarklyProvider.tsx). |
6 | | -2. Evaluating feature flags in the [Edge Middleware](https://vercel.com/docs/concepts/functions/edge-middleware). This can be seen in [`middleware.ts`](./middleware.ts). |
| 7 | +1. **Edge Middleware** ([`proxy.ts`](./proxy.ts)) — evaluates a feature flag per request |
| 8 | + and attaches the result as a header for server-rendering. |
| 9 | +2. **Edge Route Handler** ([`app/api/flag/route.ts`](./app/api/flag/route.ts)) — evaluates a |
| 10 | + feature flag and returns JSON, used by the client to poll for live updates. |
7 | 11 |
|
8 | | -## Demo |
9 | | - |
10 | | -https://hello-vercel-edge.vercel.app/ |
| 12 | +Both share a single edge client defined in [`lib/ldEdgeClient.ts`](./lib/ldEdgeClient.ts). |
11 | 13 |
|
12 | 14 | ## Local development |
13 | 15 |
|
14 | | -#### Create a new LaunchDarkly project and flags |
| 16 | +#### Create a new LaunchDarkly project and flag |
15 | 17 |
|
16 | | -For simplicity, we recommend [creating a new LaunchDarkly project](https://docs.launchdarkly.com/home/organize/projects/?q=create+proj) for this example app. After creating a new project, create the following feature flags with Client-side SDK availability: |
| 18 | +For simplicity, we recommend |
| 19 | +[creating a new LaunchDarkly project](https://docs.launchdarkly.com/home/organize/projects/?q=create+proj) |
| 20 | +for this example app. After creating a new project, create a single boolean feature flag with |
| 21 | +client-side SDK availability: |
17 | 22 |
|
18 | | -- `bootstrap-flags` - (Boolean) - This flag will determine whether or not the LaunchDarkly React SDK will bootstrap feature flags from the edge. |
19 | | -- `show-debugging-info` - (Boolean) - This flag is used to expose the current flag values. |
20 | | -- `hero-text` - (String) - This flag is used to dynamically change the hero text. You can make the variations anything you want, e.g. "The best way to buy the products you love." |
21 | | -- `enable-hot-dog-favicon` - (Boolean) - This flag is used in middleware.ts to dynamically load a different favicon. |
22 | | -- `store-closed` - (Boolean) - This flag is evaluated in `middleware.ts` and can be used to load a different home page when the store is closed. |
| 23 | +- `sample-feature` — (Boolean) the flag this example evaluates and renders. |
23 | 24 |
|
24 | 25 | #### Set up the LaunchDarkly Vercel integration |
25 | 26 |
|
26 | | -You will need to have the LaunchDarkly Vercel integration configured to push feature flag data to your Vercel Edge Config. Read the [Vercel documentation](https://docs.launchdarkly.com/integrations/vercel/) to set up the integration. Be sure to connect the project you created above. |
| 27 | +You will need to have the LaunchDarkly Vercel integration configured to push feature flag data to |
| 28 | +your Vercel Edge Config. Read the |
| 29 | +[Vercel documentation](https://docs.launchdarkly.com/integrations/vercel/) to set up the |
| 30 | +integration. Be sure to connect the project you created above. |
27 | 31 |
|
28 | 32 | #### Set up environment variables |
29 | 33 |
|
30 | | -1. Copy this directory in a new repository. |
| 34 | +1. Copy this directory into a new repository. |
31 | 35 | 2. Create a new Vercel project based on the new repository. |
32 | | -3. [Add a new environment variable to your project](https://vercel.com/docs/concepts/projects/environment-variables) named `LD_CLIENT_SIDE_ID` and set it to the LaunchDarkly client-side ID for the **Test** environment in the project you created above. |
33 | | -4. Follow [Vercel's documentation](https://vercel.com/docs/storage/edge-config/get-started) to connect an Edge Config to your new project. |
| 36 | +3. [Add a new environment variable to your project](https://vercel.com/docs/concepts/projects/environment-variables) |
| 37 | + named `LD_CLIENT_SIDE_ID` and set it to the LaunchDarkly client-side ID for the **Test** |
| 38 | + environment in the project you created above. |
| 39 | +4. Follow [Vercel's documentation](https://vercel.com/docs/storage/edge-config/get-started) to |
| 40 | + connect an Edge Config to your new project. |
34 | 41 | 5. Run the following command to link your local codebase to your Vercel project: |
35 | 42 |
|
36 | | -```shell |
37 | | -vercel link |
38 | | -``` |
| 43 | + ```shell |
| 44 | + vercel link |
| 45 | + ``` |
39 | 46 |
|
40 | | -6. Run the following command to sync your projects environment variables in your development environment: |
| 47 | +6. Run the following command to sync your project's environment variables in your development |
| 48 | + environment: |
41 | 49 |
|
42 | | -```shell |
43 | | -vercel env pull .env.development.local |
44 | | -``` |
| 50 | + ```shell |
| 51 | + vercel env pull .env.development.local |
| 52 | + ``` |
45 | 53 |
|
46 | | -7. After completing the guide above, you should have linked this example app to your Vercel project and created an `.env.development.local`. |
47 | | -8. Verify the contents of `.env.development.local` have values for the `LD_CLIENT_SIDE_ID` and `EDGE_CONFIG`. |
| 54 | +7. After completing the steps above, you should have linked this example app to your Vercel |
| 55 | + project and created a `.env.development.local`. |
| 56 | +8. Verify the contents of `.env.development.local` have values for `LD_CLIENT_SIDE_ID` and |
| 57 | + `EDGE_CONFIG`. |
48 | 58 | 9. Run the following command to install all dependencies: |
49 | 59 |
|
50 | | -```shell |
51 | | -yarn |
52 | | -``` |
| 60 | + ```shell |
| 61 | + yarn |
| 62 | + ``` |
53 | 63 |
|
54 | 64 | 10. Run the following command to start your development environment: |
55 | 65 |
|
| 66 | + ```shell |
| 67 | + yarn dev |
| 68 | + ``` |
| 69 | + |
| 70 | +Open [http://localhost:3000](http://localhost:3000). You should see: |
| 71 | + |
| 72 | +- **Green background** (`#00844B`) when the flag evaluates to `true` |
| 73 | +- **Dark background** (`#373841`) when the flag evaluates to `false` |
| 74 | +- The message: "The sample-feature feature flag evaluates to true/false." |
| 75 | + |
| 76 | +The page polls `/api/flag` every 2 seconds. Toggle the flag in LaunchDarkly and the background |
| 77 | +color will update automatically. |
| 78 | + |
| 79 | +## Optional: try it without a Vercel account |
| 80 | + |
| 81 | +If you want to run the example locally without setting up the LaunchDarkly Vercel integration, the |
| 82 | +example includes a built-in mock Edge Config server. Copy the pre-configured mock environment file |
| 83 | +and start the dev server: |
| 84 | + |
56 | 85 | ```shell |
| 86 | +cp .env.mock .env.local |
57 | 87 | yarn dev |
58 | 88 | ``` |
| 89 | + |
| 90 | +Mock mode uses the |
| 91 | +[external connection string](https://vercel.com/docs/edge-config) feature of `@vercel/edge-config` |
| 92 | +to point the SDK at a local API route ([`app/api/edge-config-mock/[...path]/route.ts`](./app/api/edge-config-mock/%5B...path%5D/route.ts)) |
| 93 | +that returns a static flag payload. This is for local development only — production deployments |
| 94 | +should use a real Vercel Edge Config. |
| 95 | + |
| 96 | +## How it works |
| 97 | + |
| 98 | +| Path | Purpose | |
| 99 | +|------|---------| |
| 100 | +| [`proxy.ts`](./proxy.ts) | Edge Middleware that evaluates the flag and sets a header for server-rendering. | |
| 101 | +| [`app/api/flag/route.ts`](./app/api/flag/route.ts) | Edge Route Handler that evaluates the flag and returns JSON for client polling. | |
| 102 | +| [`app/page.tsx`](./app/page.tsx) | Server component that reads the middleware header for the initial render. | |
| 103 | +| [`app/FlagDisplay.tsx`](./app/FlagDisplay.tsx) | Client component that polls `/api/flag` every 2 seconds for live updates. | |
| 104 | +| [`lib/ldEdgeClient.ts`](./lib/ldEdgeClient.ts) | Lazily-initialized LaunchDarkly Vercel SDK client shared by middleware and route handler. | |
| 105 | +| [`app/api/edge-config-mock/`](./app/api/edge-config-mock/) | Mock Edge Config server for local development without a Vercel account. | |
0 commit comments