Skip to content

Commit 2296c4f

Browse files
ldhenrymonsagri
andauthored
feat: update Vercel example (#129)
This PR clean up our Vercel example with improved documentation, updated dependencies and a simplified example. I am planning to add a more complete example in a separate PR. --------- Co-authored-by: Fabian <[email protected]>
1 parent 8475c08 commit 2296c4f

File tree

6 files changed

+192
-65
lines changed

6 files changed

+192
-65
lines changed
Lines changed: 121 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,131 @@
1-
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
lerna-debug.log*
8+
.pnpm-debug.log*
29

3-
# dependencies
4-
/node_modules
5-
/.pnp
6-
.pnp.js
10+
# Diagnostic reports (https://nodejs.org/api/report.html)
11+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
712

8-
# testing
9-
/coverage
13+
# Runtime data
14+
pids
15+
*.pid
16+
*.seed
17+
*.pid.lock
1018

11-
# next.js
12-
/.next/
13-
/out/
19+
# Directory for instrumented libs generated by jscoverage/JSCover
20+
lib-cov
1421

15-
# production
16-
/build
22+
# Coverage directory used by tools like istanbul
23+
coverage
24+
*.lcov
1725

18-
# misc
19-
.DS_Store
20-
*.pem
26+
# nyc test coverage
27+
.nyc_output
2128

22-
# debug
23-
npm-debug.log*
24-
yarn-debug.log*
25-
yarn-error.log*
29+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
30+
.grunt
2631

27-
# local env files
28-
.env*.local
32+
# Bower dependency directory (https://bower.io/)
33+
bower_components
2934

30-
# vercel
31-
.vercel
35+
# node-waf configuration
36+
.lock-wscript
37+
38+
# Compiled binary addons (https://nodejs.org/api/addons.html)
39+
build/Release
3240

33-
# typescript
41+
# Dependency directories
42+
node_modules/
43+
jspm_packages/
44+
45+
# Snowpack dependency directory (https://snowpack.dev/)
46+
web_modules/
47+
48+
# TypeScript cache
3449
*.tsbuildinfo
35-
next-env.d.ts
36-
.vscode
50+
51+
# Optional npm cache directory
52+
.npm
53+
54+
# Optional eslint cache
55+
.eslintcache
56+
57+
# Optional stylelint cache
58+
.stylelintcache
59+
60+
# Microbundle cache
61+
.rpt2_cache/
62+
.rts2_cache_cjs/
63+
.rts2_cache_es/
64+
.rts2_cache_umd/
65+
66+
# Optional REPL history
67+
.node_repl_history
68+
69+
# Output of 'npm pack'
70+
*.tgz
71+
72+
# Yarn Integrity file
73+
.yarn-integrity
74+
75+
# dotenv environment variable files
76+
.env
77+
.env.development.local
78+
.env.test.local
79+
.env.production.local
80+
.env.local
81+
82+
# parcel-bundler cache (https://parceljs.org/)
83+
.cache
84+
.parcel-cache
85+
86+
# Next.js build output
87+
.next
88+
out
89+
90+
# Nuxt.js build / generate output
91+
.nuxt
92+
dist
93+
94+
# Gatsby files
95+
.cache/
96+
# Comment in the public line in if your project uses Gatsby and not Next.js
97+
# https://nextjs.org/blog/next-9-1#public-directory-support
98+
# public
99+
100+
# vuepress build output
101+
.vuepress/dist
102+
103+
# vuepress v2.x temp and cache directory
104+
.temp
105+
.cache
106+
107+
# Docusaurus cache and generated files
108+
.docusaurus
109+
110+
# Serverless directories
111+
.serverless/
112+
113+
# FuseBox cache
114+
.fusebox/
115+
116+
# DynamoDB Local files
117+
.dynamodb/
118+
119+
# TernJS port file
120+
.tern-port
121+
122+
# Stores VSCode versions used for testing VSCode extensions
123+
.vscode-test
124+
125+
# yarn v2
126+
.yarn/cache
127+
.yarn/unplugged
128+
.yarn/build-state.yml
129+
.yarn/install-state.gz
130+
.pnp.*
131+
.vercel
Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,54 @@
11
# Example test app for Vercel LaunchDarkly SDK
22

33
This is an example test app to showcase the usage of the Vercel LaunchDarkly
4-
SDK. This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
4+
SDK to evaluate a feature flag in a [Route Handler](https://nextjs.org/docs/app/building-your-application/routing/router-handlers) using [Vercel's edge runtime](https://nextjs.org/docs/app/building-your-application/routing/router-handlers#edge-and-nodejs-runtimes). This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
5+
6+
Most of the LaunchDarkly-related code can be found in [src/app/api/hello/route.ts](src/app/api/hello/route.ts).
57

68
## Prerequisites
79

810
A node environment of version 16 and yarn are required to develop in this repository.
9-
You will also need the vercel cli installed and a Vercel account to setup
10-
the test data required by this example. See the [Vercel docs](https://vercel.com/docs/storage/edge-config/get-started) on how to setup your edge config store.
11+
You will also need the [Vercel CLI](https://vercel.com/docs/cli) installed and a Vercel account to setup
12+
the test data required by this example. See the [Vercel docs](https://vercel.com/docs/storage/edge-config/get-started) on how to setup your Edge Config store.
13+
14+
## Setting up your LaunchDarkly environment
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:
1117

12-
## Usage
18+
- `test-flag` - (Boolean) - This flag is evaluated in [src/app/api/hello/route.ts](src/app/api/hello/route.ts).
1319

14-
1. Follow the [Vercel docs](https://vercel.com/docs/storage/edge-config/get-started) to insert [testData.json](https://github.com/launchdarkly/js-core/blob/main/packages/sdk/vercel/testData.json) to your edge config store.
20+
After creating your project, you will need to configure the LaunchDarkly Vercel integration to push feature flag data to your Vercel Edge Config. Read the [Vercel integration documentation](https://docs.launchdarkly.com/integrations/vercel/) to set up the integration. Be sure to connect the **Test** environment from the project you created above.
1521

16-
2. After completing the guide above, you should have linked this example app to your Vercel project and created an `.env.development.local`.
22+
## Setting up your development environment
23+
24+
1. Copy this directory in a new repository.
25+
2. Create a new Vercel project based on the new repository.
26+
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.
27+
4. Follow [Vercel's documentation](https://vercel.com/docs/storage/edge-config/get-started) to connect an Edge Config to your new project.
28+
5. Run the following command to link your local codebase to your Vercel project:
29+
30+
```shell
31+
vercel link
32+
```
33+
34+
6. Run the following command to sync your projects environment variables in your development environment:
35+
36+
```shell
37+
vercel env pull .env.development.local
38+
```
1739

18-
3. At the root of the js-core repo:
40+
7. After completing the guide above, you should have linked this example app to your Vercel project and created an `.env.development.local`.
41+
8. Verify the contents of `.env.development.local` have values for the `LD_CLIENT_SIDE_ID` and `EDGE_CONFIG`.
42+
9. Run the following command to install all dependencies:
1943

2044
```shell
21-
yarn && yarn build
45+
yarn
2246
```
2347

24-
4. Then back in this example folder:
48+
10. Run the following command to start your development environment:
2549

2650
```shell
2751
yarn dev
2852
```
2953

30-
5. Open [http://localhost:3000/api/hello](http://localhost:3000/api/hello).
54+
11. Open [http://localhost:3000/api/hello](http://localhost:3000/api/hello).
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/basic-features/typescript for more information.

packages/sdk/vercel/example-vercel/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@
44
"packageManager": "[email protected]",
55
"scripts": {
66
"dev": "next dev",
7-
"build": "tsc",
7+
"build": "next build",
88
"start": "next start",
99
"lint": "next lint"
1010
},
1111
"dependencies": {
1212
"@launchdarkly/vercel-server-sdk": "0.3.0",
13-
"@vercel/edge-config": "^0.1.8",
14-
"next": "13.3.1",
15-
"react": "18.2.0",
13+
"@vercel/edge-config": "^0.2.1",
14+
"next": "^13.4.4",
15+
"react": "^18.2.0",
1616
"react-dom": "18.2.0"
1717
},
1818
"devDependencies": {
1919
"@types/node": "18.16.2",
20-
"@types/react": "18.2.0",
21-
"@types/react-dom": "18.2.1",
20+
"@types/react": "^18.2.7",
21+
"@types/react-dom": "^18.2.4",
2222
"autoprefixer": "10.4.14",
2323
"postcss": "8.4.23",
2424
"tailwindcss": "3.3.2",

packages/sdk/vercel/example-vercel/src/app/api/hello/route.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,40 @@ import { NextResponse } from 'next/server';
22
import { createClient } from '@vercel/edge-config';
33
import { init as initLD } from '@launchdarkly/vercel-server-sdk';
44

5-
export const config = {
6-
runtime: 'edge',
7-
};
5+
export const runtime = 'edge';
86

97
export async function GET() {
10-
const sdkKey = 'test-sdk-key';
11-
const flagKey = 'testFlag1';
8+
const clientSideID = process.env.LD_CLIENT_SIDE_ID;
9+
if (!clientSideID) {
10+
return NextResponse.json(
11+
{
12+
error: 'LD_CLIENT_SIDE_ID environment variable is missing.',
13+
},
14+
{ status: 500 }
15+
);
16+
}
17+
const flagKey = 'test-flag';
1218

13-
// default fallthrough evaluates to true
14-
const contextFallthrough = { kind: 'user', key: 'test-user-key-1' };
15-
16-
// matches email targeting rule evaluates to false
17-
const contextEmailRule = { kind: 'user', key: 'test-user-key-1', email: '[email protected]' };
19+
// This is just a simple example context. You can also include request information such as headers and path or user ID
20+
// if the request is authenticated.
21+
const flagContext = { kind: 'user', key: 'test-user-key-1' };
1822

1923
const vercelClient = createClient(process.env.EDGE_CONFIG);
2024

21-
// start using ld
22-
const client = initLD(sdkKey, vercelClient);
25+
const client = initLD(clientSideID, vercelClient);
2326
await client.waitForInitialization();
24-
const fallthrough = await client.variation(flagKey, contextFallthrough, false);
25-
const emailRule = await client.variation(flagKey, contextEmailRule, false);
26-
const flagDetail = await client.variationDetail(flagKey, contextEmailRule, false);
27-
const allFlags = await client.allFlagsState(contextEmailRule);
27+
28+
// Use .variation() to evaluate a single feature flag for a given context.
29+
const testFlagVariation = await client.variation(flagKey, flagContext, false);
30+
31+
// allFlagsState() returns an object containing the variations served for all feature flags for a given context.
32+
// This is useful for bootstrapping flags for use in the LaunchDarkly React SDK or JS client-side SDK.
33+
const allFlags = await client.allFlagsState(flagContext);
2834

2935
return NextResponse.json({
3036
flagKey: `${flagKey}`,
31-
contextKey: `${contextFallthrough.key}`,
32-
fallthrough,
33-
emailRule,
34-
emailRuleDetail: `${JSON.stringify(flagDetail)}`,
35-
allFlags: `${JSON.stringify(allFlags)}`,
37+
context: flagContext,
38+
variationServed: testFlagVariation,
39+
allFlags: allFlags.toJSON(),
3640
});
3741
}

packages/sdk/vercel/example-vercel/src/app/page.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ export default function Home() {
88
<main className="flex min-h-screen flex-col items-center justify-between p-24">
99
<div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex">
1010
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
11-
Get started by editing&nbsp;
12-
<code className="font-mono font-bold">src/app/page.tsx</code>
11+
Get started by visiting&nbsp;<a href="/api/hello">/api/hello</a>
1312
</p>
1413
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
1514
<a

0 commit comments

Comments
 (0)