Skip to content

Commit 138cc06

Browse files
authored
chore: Scaffold universal sdk (#492)
As part of moonshots, I'm aiming to release an alpha version of a universal react sdk which can work with App Router.
1 parent ceed47a commit 138cc06

File tree

17 files changed

+360
-0
lines changed

17 files changed

+360
-0
lines changed

.github/workflows/manual-publish.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ on:
2424
- packages/sdk/cloudflare
2525
- packages/sdk/react-native
2626
- packages/sdk/server-node
27+
- packages/sdk/react-universal
2728
- packages/sdk/vercel
2829
- packages/sdk/akamai-base
2930
- packages/sdk/akamai-edgekv
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: sdk/react-universal
2+
3+
on:
4+
push:
5+
branches: [main, 'feat/**']
6+
paths-ignore:
7+
- '**.md' #Do not need to run CI for markdown changes.
8+
pull_request:
9+
branches: [main, 'feat/**']
10+
paths-ignore:
11+
- '**.md'
12+
13+
jobs:
14+
build-test-react-universal:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: actions/checkout@v4
18+
- uses: actions/setup-node@v4
19+
- id: shared
20+
name: Shared CI Steps
21+
uses: ./actions/ci
22+
with:
23+
workspace_name: '@launchdarkly/react-universal-sdk'
24+
workspace_path: packages/sdk/react-universal

.github/workflows/release-please.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
package-node-server-sdk-dynamodb-release: ${{ steps.release.outputs['packages/store/node-server-sdk-dynamodb--release_created'] }}
2424
package-node-server-sdk-otel-release: ${{ steps.release.outputs['packages/telemetry/node-server-sdk-otel--release_created'] }}
2525
package-tooling-jest-release: ${{ steps.release.outputs['packages/tooling/jest--release_created'] }}
26+
package-react-universal-release: ${{ steps.release.outputs['packages/sdk/react-universal--release_created'] }}
2627
steps:
2728
- uses: googleapis/release-please-action@v4
2829
id: release
@@ -335,3 +336,24 @@ jobs:
335336
with:
336337
workspace_path: packages/tooling/jest
337338
aws_assume_role: ${{ vars.AWS_ROLE_ARN }}
339+
340+
release-tooling-react-universal:
341+
runs-on: ubuntu-latest
342+
needs: ['release-please']
343+
permissions:
344+
id-token: write
345+
contents: write
346+
# HACK: react-universal sdk is not ready for release yet.
347+
if: false #${{ needs.release-please.outputs.package-react-universal-release == 'true' }}
348+
steps:
349+
- uses: actions/checkout@v4
350+
- uses: actions/setup-node@v4
351+
with:
352+
node-version: 20.x
353+
registry-url: 'https://registry.npmjs.org'
354+
- id: release-react-universal-sdk
355+
name: Full release of packages/sdk/react-universal
356+
uses: ./actions/full-release
357+
with:
358+
workspace_path: packages/sdk/react-universal
359+
aws_assume_role: ${{ vars.AWS_ROLE_ARN }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"packages/sdk/cloudflare/example",
1313
"packages/sdk/react-native",
1414
"packages/sdk/react-native/example",
15+
"packages/sdk/react-universal",
1516
"packages/sdk/vercel",
1617
"packages/sdk/akamai-base",
1718
"packages/sdk/akamai-base/example",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Changelog
2+
3+
All notable changes to the LaunchDarkly React Universal package will be documented in this file. This project adheres to [Semantic Versioning](https://semver.org).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2024 Catamorphic, Co.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# LaunchDarkly React Universal SDK
2+
3+
[![NPM][react-universal-sdk-npm-badge]][react-universal-sdk-npm-link]
4+
[![Actions Status][react-universal-sdk-ci-badge]][react-universal-sdk-ci]
5+
[![Documentation][react-universal-sdk-ghp-badge]][react-universal-sdk-ghp-link]
6+
[![NPM][react-universal-sdk-dm-badge]][react-universal-sdk-npm-link]
7+
[![NPM][react-universal-sdk-dt-badge]][react-universal-sdk-npm-link]
8+
9+
> [!CAUTION]
10+
> This library is a beta version and should not be considered ready for production use while this message is visible.
11+
12+
> **An idiomatic LaunchDarkly SDK which supports RSC, server side rendering and bootstrapping** :clap:
13+
14+
This SDK supports:
15+
16+
- React Server Components
17+
- Server side rendering
18+
- Bootstrapping
19+
20+
## Installation
21+
22+
```shell
23+
# npm
24+
npm i @launchdarkly/react-universal-sdk --save-dev
25+
26+
# yarn
27+
yarn add -D @launchdarkly/react-universal-sdk
28+
```
29+
30+
### Server API
31+
32+
- `initNodeSdk` - Initializes the Node SDK on server startup using the [instrumentation hook](https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation)
33+
34+
- `getBootstrap` - Returns a json suitable for bootstrapping the js sdk.
35+
36+
- `useLDClientRsc` - Use this to get an ldClient for Server Components.
37+
38+
### Client API
39+
40+
- `LDProvider` - The react context provider.
41+
42+
- `useLDClient` - Use this to get an ldClient for Client Components.
43+
44+
## Usage
45+
46+
1. Enable [instrumentationHook](https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation) in `next.config.mjs`:
47+
48+
```ts
49+
/** @type {import('next').NextConfig} */
50+
const nextConfig = {
51+
experimental: { instrumentationHook: true },
52+
};
53+
54+
export default nextConfig;
55+
```
56+
57+
2. Create a new file `instrumentation.ts` at the root of your project. This will initialize the Node Server SDK.
58+
59+
```ts
60+
import { initNodeSdk } from '@/ld/server';
61+
62+
export async function register() {
63+
await initNodeSdk();
64+
}
65+
```
66+
67+
3. In your root layout component, render the `LDProvider` using your `LDContext` and `bootstrap`:
68+
69+
```tsx
70+
export default async function RootLayout({
71+
children,
72+
}: Readonly<{
73+
children: ReactNode;
74+
}>) {
75+
// You must supply an LDContext. For example, here getLDContext
76+
// inspects cookies and defaults to anonymous.
77+
const context = getLDContext();
78+
79+
// A bootstrap is required to initialize LDProvider.
80+
const bootstrap = await getBootstrap(context);
81+
82+
return (
83+
<html lang="en">
84+
<body className={inter.className}>
85+
<LDProvider context={context} options={{ bootstrap }}>
86+
{children}
87+
</LDProvider>
88+
</body>
89+
</html>
90+
);
91+
}
92+
```
93+
94+
4. Server Components must use the async `useLDClientRsc` function:
95+
96+
```tsx
97+
// You should use your own getLDContext function.
98+
import { getLDContext } from '@/app/utils';
99+
import { useLDClientRsc } from '@/ld/server';
100+
101+
export default async function Page() {
102+
const ldc = await useLDClientRsc(getLDContext());
103+
const flagValue = ldc.variation('dev-test-flag');
104+
105+
return (
106+
<main className="flex min-h-screen flex-col items-center justify-between p-24">
107+
Server Component: {flagValue.toString()}
108+
</main>
109+
);
110+
}
111+
```
112+
113+
5. Client Components must use the `useLDClient` hook:
114+
115+
```tsx
116+
'use client';
117+
118+
import { useLDClient } from '@/ld/client';
119+
120+
export default function LDButton() {
121+
const ldc = useLDClient();
122+
const flagValue = ldc.variation('dev-test-flag');
123+
124+
return <p>Client Component: {flagValue.toString()}</p>;
125+
}
126+
```
127+
128+
You will see both components are rendered on the server (view source on your browser). However, only Client Components
129+
will respond to live changes.
130+
131+
## Verifying SDK build provenance with the SLSA framework
132+
133+
LaunchDarkly uses the [SLSA framework](https://slsa.dev/spec/v1.0/about) (Supply-chain Levels for Software Artifacts) to help developers make their supply chain more secure by ensuring the authenticity and build integrity of our published SDK packages. To learn more, see the [provenance guide](PROVENANCE.md).
134+
135+
## About LaunchDarkly
136+
137+
- LaunchDarkly is a continuous delivery platform that provides feature flags as a service and allows developers to iterate quickly and safely. We allow you to easily flag your features and manage them from the LaunchDarkly dashboard. With LaunchDarkly, you can:
138+
- Roll out a new feature to a subset of your users (like a group of users who opt-in to a beta tester group), gathering feedback and bug reports from real-world use cases.
139+
- Gradually roll out a feature to an increasing percentage of users, and track the effect that the feature has on key metrics (for instance, how likely is a user to complete a purchase if they have feature A versus feature B?).
140+
- Turn off a feature that you realize is causing performance problems in production, without needing to re-deploy, or even restart the application with a changed configuration file.
141+
- Grant access to certain features based on user attributes, like payment plan (eg: users on the ‘gold’ plan get access to more features than users in the ‘silver’ plan).
142+
- Disable parts of your application to facilitate maintenance, without taking everything offline.
143+
- LaunchDarkly provides feature flag SDKs for a wide variety of languages and technologies. Read [our documentation](https://docs.launchdarkly.com/sdk) for a complete list.
144+
- Explore LaunchDarkly
145+
- [launchdarkly.com](https://www.launchdarkly.com/ 'LaunchDarkly Main Website') for more information
146+
- [docs.launchdarkly.com](https://docs.launchdarkly.com/ 'LaunchDarkly Documentation') for our documentation and SDK reference guides
147+
- [apidocs.launchdarkly.com](https://apidocs.launchdarkly.com/ 'LaunchDarkly API Documentation') for our API documentation
148+
- [blog.launchdarkly.com](https://blog.launchdarkly.com/ 'LaunchDarkly Blog Documentation') for the latest product updates
149+
150+
[react-universal-sdk-ci-badge]: https://github.com/launchdarkly/js-core/actions/workflows/react-universal-sdk.yml/badge.svg
151+
[react-universal-sdk-ci]: https://github.com/launchdarkly/js-core/actions/workflows/react-universal-sdk.yml
152+
[react-universal-sdk-npm-badge]: https://img.shields.io/npm/v/@launchdarkly/react-universal-sdk.svg?style=flat-square
153+
[react-universal-sdk-npm-link]: https://www.npmjs.com/package/@launchdarkly/react-universal-sdk
154+
[react-universal-sdk-ghp-badge]: https://img.shields.io/static/v1?label=GitHub+Pages&message=API+reference&color=00add8
155+
[react-universal-sdk-ghp-link]: https://launchdarkly.github.io/js-core/packages/tooling/react-universal-sdk/docs/
156+
[react-universal-sdk-dm-badge]: https://img.shields.io/npm/dm/@launchdarkly/react-universal-sdk.svg?style=flat-square
157+
[react-universal-sdk-dt-badge]: https://img.shields.io/npm/dt/@launchdarkly/react-universal-sdk.svg?style=flat-square
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# LaunchDarkly Universal SDK example
2+
3+
> [!IMPORTANT]
4+
> This is an experimental project to demonstrate the use of LaunchDarkly with Next.js App Router.
5+
>
6+
> This is designed for the App Router. Pages router is not supported.
7+
8+
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) using App Router.
9+
10+
## Quickstart
11+
12+
To run this project:
13+
14+
1. Create an .env file at repo root.
15+
2. Add your SDK key and client-side ID:
16+
17+
```dotenv
18+
LD_SDK_KEY=sdk-***
19+
NEXT_PUBLIC_LD_CLIENT_SIDE_ID=***
20+
```
21+
22+
3. Replace `dev-test-flag` with your own flags in `app.tsx` and `LDButton.tsx`.
23+
4. `yarn && yarn dev`
24+
25+
You should see your flag value rendered in the browser.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"transform": { "^.+\\.ts?$": "ts-jest" },
3+
"testMatch": ["**/*.test.ts?(x)"],
4+
"testPathIgnorePatterns": ["node_modules", "example", "dist"],
5+
"modulePathIgnorePatterns": ["dist"],
6+
"testEnvironment": "node",
7+
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"],
8+
"collectCoverageFrom": ["src/**/*.ts"]
9+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"name": "@launchdarkly/react-universal-sdk",
3+
"version": "0.0.1",
4+
"description": "React Universal LaunchDarkly SDK",
5+
"homepage": "https://github.com/launchdarkly/js-core/tree/main/packages/sdk/react-universal",
6+
"repository": {
7+
"type": "git",
8+
"url": "https://github.com/launchdarkly/js-core.git"
9+
},
10+
"license": "Apache-2.0",
11+
"packageManager": "[email protected]",
12+
"keywords": [
13+
"launchdarkly",
14+
"react",
15+
"universal",
16+
"nextjs",
17+
"remix"
18+
],
19+
"type": "module",
20+
"exports": {
21+
".": {
22+
"types": "./dist/src/index.d.ts",
23+
"default": "./dist/src/index.js"
24+
}
25+
},
26+
"files": [
27+
"dist"
28+
],
29+
"scripts": {
30+
"clean": "rimraf dist",
31+
"build": "yarn clean && tsc",
32+
"tsw": "yarn tsc --watch",
33+
"start": "rimraf dist && yarn tsw",
34+
"lint": "eslint . --ext .ts",
35+
"prettier": "prettier --write '**/*.@(js|ts|tsx|json|css)' --ignore-path ../../../.prettierignore",
36+
"test": "NODE_OPTIONS=\"--experimental-vm-modules --no-warnings\" jest --ci --runInBand",
37+
"coverage": "yarn test --coverage",
38+
"check": "yarn prettier && yarn lint && yarn build && yarn test"
39+
},
40+
"devDependencies": {
41+
"@trivago/prettier-plugin-sort-imports": "^4.1.1",
42+
"@types/jest": "^29.5.0",
43+
"@typescript-eslint/eslint-plugin": "^6.20.0",
44+
"@typescript-eslint/parser": "^6.20.0",
45+
"eslint": "^8.45.0",
46+
"eslint-config-airbnb-base": "^15.0.0",
47+
"eslint-config-airbnb-typescript": "^17.1.0",
48+
"eslint-config-prettier": "^8.8.0",
49+
"eslint-plugin-import": "^2.27.5",
50+
"eslint-plugin-jest": "^27.6.3",
51+
"eslint-plugin-prettier": "^5.0.0",
52+
"jest": "^29.5.0",
53+
"prettier": "^3.0.0",
54+
"rimraf": "^5.0.1",
55+
"ts-jest": "^29.1.0",
56+
"typedoc": "0.25.0",
57+
"typescript": "5.1.6"
58+
}
59+
}

0 commit comments

Comments
 (0)