Skip to content

Commit 30d81c6

Browse files
committed
chore: addressing PR comments
1 parent 22f3a41 commit 30d81c6

File tree

4 files changed

+94
-41
lines changed

4 files changed

+94
-41
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Set LD_CLIENT_SIDE_ID to your LaunchDarkly client-side ID
2+
LD_CLIENT_SIDE_ID=
3+
4+
# Set LD_FLAG_KEY to the feature flag key you want to evaluate
5+
LD_FLAG_KEY=

packages/sdk/browser/example/README.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,30 @@ We've built a simple browser application that demonstrates how this LaunchDarkly
1313
Below, you'll find the build procedure. For more comprehensive instructions, you can visit your [Quickstart page](https://app.launchdarkly.com/quickstart#/) or
1414
the [{name of SDK} reference guide](https://docs.launchdarkly.com/sdk/client-side/javascript).
1515

16+
## Prerequisites
17+
18+
Nodejs 20.6.0 or later
19+
1620
## Build instructions
1721

18-
Modify [app.ts](./src/app.ts) with the following changes:
22+
1. Make a copy of the `.env.template` and name it `.env`
23+
```
24+
cp .env.template .env
25+
```
1926
20-
1. Set the value of the {`clientSideID`} variable in {file name} to your client-side ID:
21-
```typescript
22-
const clientSideID = "my-client-side-id";
27+
2. Set the variables in `.env` to your specific LD values
2328
```
24-
25-
2. If there is an existing boolean feature flag in your LaunchDarkly project that
26-
you want to evaluate, set `flagKey` to the flag key:
27-
```typescript
28-
const flagKey = "my-flag-key";
29+
# Set LD_CLIENT_SIDE_ID to your LaunchDarkly client-side ID
30+
LD_CLIENT_SIDE_ID=
31+
32+
# Set LD_FLAG_KEY to the feature flag key you want to evaluate
33+
LD_FLAG_KEY=
2934
```
35+
> [!NOTE]
36+
> Setting these values is equivilent to modifying the `clientSideID` and `flagKey`
37+
> in [app.ts](./src/app.ts).
3038
31-
3. If you haven't already, install and build the project:
39+
3. Install and build the project:
3240
```bash
3341
yarn && yarn build
3442
```
@@ -41,4 +49,4 @@ Modify [app.ts](./src/app.ts) with the following changes:
4149
> The `yarn start` script simply runs `open index.html`. If that is not working for you,
4250
> you can open the `index.html` file in a browser for the same results.
4351
44-
The application will run continuously and react to the flag changes in LaunchDarkly.
52+
The application will run continuously and react to the flag changes in LaunchDarkly.
Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { initialize } from '@launchdarkly/js-client-sdk';
22

33
// Set clientSideID to your LaunchDarkly client-side ID
4-
const clientSideID = '';
4+
const clientSideID = 'LD_CLIENT_SIDE_ID';
55

66
// Set flagKey to the feature flag key you want to evaluate
7-
const flagKey = 'sample-feature';
7+
const flagKey = 'LD_FLAG_KEY';
88

99
// Set up the evaluation context. This context should appear on your
1010
// LaunchDarkly contexts dashboard soon after you run the demo.
@@ -17,30 +17,42 @@ const context = {
1717
const div = document.createElement('div');
1818
document.body.appendChild(div);
1919
div.appendChild(document.createTextNode('Initializing...'));
20-
try {
21-
const ldclient = initialize(clientSideID);
22-
const render = () => {
23-
const flagValue = ldclient.variation(flagKey, false);
24-
const label = `The ${flagKey} feature flag evaluates to ${flagValue}.`;
25-
document.body.style.background = flagValue ? '#00844B' : '#373841';
26-
div.replaceChild(document.createTextNode(label), div.firstChild as Node);
27-
};
28-
29-
ldclient.on('error', () => {
30-
div.replaceChild(document.createTextNode('Error caught in client SDK'), div.firstChild as Node);
31-
});
32-
33-
ldclient.on('change', () => {
34-
render();
35-
});
36-
37-
ldclient.identify(context).catch(() => {
38-
div.replaceChild(document.createTextNode('Error identifying client'), div.firstChild as Node);
39-
});
40-
} catch (error) {
41-
div.replaceChild(
42-
document.createTextNode(`Error initializing LaunchDarkly client: ${error}`),
43-
div.firstChild as Node,
44-
);
45-
document.body.style.background = '#373841';
46-
}
20+
21+
const main = async () => {
22+
try {
23+
const ldclient = initialize(clientSideID);
24+
const render = () => {
25+
const flagValue = ldclient.variation(flagKey, false);
26+
const label = `The ${flagKey} feature flag evaluates to ${flagValue}.`;
27+
document.body.style.background = flagValue ? '#00844B' : '#373841';
28+
div.replaceChild(document.createTextNode(label), div.firstChild as Node);
29+
};
30+
31+
ldclient.on('error', () => {
32+
div.replaceChild(
33+
document.createTextNode('Error caught in client SDK'),
34+
div.firstChild as Node,
35+
);
36+
});
37+
38+
// Listen for flag changes
39+
ldclient.on('change', () => {
40+
render();
41+
});
42+
43+
const { status } = await ldclient.identify(context);
44+
if (status === 'completed') {
45+
render();
46+
} else if (status === 'error') {
47+
div.replaceChild(document.createTextNode('Error identifying client'), div.firstChild as Node);
48+
}
49+
} catch (error) {
50+
div.replaceChild(
51+
document.createTextNode(`Error initializing LaunchDarkly client: ${error}`),
52+
div.firstChild as Node,
53+
);
54+
document.body.style.background = '#373841';
55+
}
56+
};
57+
58+
main();
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
11
/* eslint-disable import/no-extraneous-dependencies */
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import { loadEnvFile } from 'node:process';
25
import { defineConfig } from 'tsdown';
36

7+
if (fs.existsSync('.env')) {
8+
loadEnvFile('.env');
9+
}
10+
11+
const ENTRY_FILE = path.join('src', 'app.ts');
12+
const OUTPUT_FILE = path.join('dist', 'app.js');
13+
const { LD_CLIENT_SIDE_ID, LD_FLAG_KEY } = process.env;
14+
15+
const CLIENT_SIDE_ID_PLACEHOLDER = 'LD_CLIENT_SIDE_ID';
16+
const FLAG_KEY_PLACEHOLDER = 'LD_FLAG_KEY';
17+
418
export default defineConfig({
5-
entry: 'src/app.ts',
19+
entry: ENTRY_FILE,
620
platform: 'browser',
721
outDir: 'dist',
822
noExternal: ['@launchdarkly/js-client-sdk'],
23+
hooks(hooks) {
24+
hooks.hook('build:done', () => {
25+
if (LD_CLIENT_SIDE_ID) {
26+
const content = fs.readFileSync(OUTPUT_FILE).toString();
27+
fs.writeFileSync(
28+
OUTPUT_FILE,
29+
content.replaceAll(CLIENT_SIDE_ID_PLACEHOLDER, LD_CLIENT_SIDE_ID),
30+
);
31+
}
32+
const flagKey = LD_FLAG_KEY || 'sample-feature';
33+
const content = fs.readFileSync(OUTPUT_FILE).toString();
34+
fs.writeFileSync(OUTPUT_FILE, content.replaceAll(FLAG_KEY_PLACEHOLDER, flagKey));
35+
});
36+
},
937
});

0 commit comments

Comments
 (0)