|
| 1 | +# Complete example app for Vercel LaunchDarkly SDK |
| 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: |
| 4 | + |
| 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 | + |
| 8 | +## Demo |
| 9 | + |
| 10 | +https://hello-vercel-edge.vercel.app/ |
| 11 | + |
| 12 | +## Local development |
| 13 | + |
| 14 | +#### Create a new LaunchDarkly project and flags |
| 15 | + |
| 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: |
| 17 | + |
| 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 | + |
| 24 | +#### Set up the LaunchDarkly Vercel integration |
| 25 | + |
| 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 | + |
| 28 | +#### Set up environment variables |
| 29 | + |
| 30 | +1. Copy this directory in a new repository. |
| 31 | +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. |
| 34 | +5. Run the following command to link your local codebase to your Vercel project: |
| 35 | + |
| 36 | +```shell |
| 37 | +vercel link |
| 38 | +``` |
| 39 | + |
| 40 | +6. Run the following command to sync your projects environment variables in your development environment: |
| 41 | + |
| 42 | +```shell |
| 43 | +vercel env pull .env.development.local |
| 44 | +``` |
| 45 | + |
| 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`. |
| 48 | +9. Run the following command to install all dependencies: |
| 49 | + |
| 50 | +```shell |
| 51 | +yarn |
| 52 | +``` |
| 53 | + |
| 54 | +10. Run the following command to start your development environment: |
| 55 | + |
| 56 | +```shell |
| 57 | +yarn dev |
| 58 | +``` |
0 commit comments