-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Added react-universal code from labs. #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
2546db7
chore: Initial move from ld-nextjs.
yusinto b4e31a4
fix: Include react canary types to use cache. Fixed serverCache bug. …
yusinto 1fe5bb0
chore: Added example app.
yusinto 4473b5f
chore: Fix eslint for example app.
yusinto 4034f45
Delete .eslintrc.json
yusinto 1e681df
chore: Improved readme.
yusinto 34f5452
chore: Add release please scripts.
yusinto 0aa6748
adding example content from launchdarkly/ld-nextjs
codyde 2fcbd33
Merge branch 'yus/sc-248499/move-ld-nextjs-to-js-core' of https://git…
codyde 3f75b81
chore: Ran prettier on example app. Removed extraneous prettierrc.
yusinto 5b8fc42
chore: Fix @ld imports.
yusinto 9769092
Create .eslintrc.js
yusinto d95dd13
Update globals.css
yusinto c2ac484
chore: Replace dev-test-flag with my-boolean-flag-1.
yusinto 2c0a188
chore: Remove redundant comments.
yusinto 8c2c6df
Update .eslintrc.js
yusinto efe4513
Renaming .env.local to .example.env.local and adding warning
codyde 7cd1384
Merge branch 'yus/sc-248499/move-ld-nextjs-to-js-core' of https://git…
codyde 141bb41
Create .eslintignore
yusinto 5ebdf31
chore: Added login and cookie example.
yusinto b2dc0c1
Update release-please.yml
yusinto 1f9e52d
Update release-please-config.json
yusinto ef5c89d
chore: Improved readme.
yusinto 1fc710a
Update package.json
yusinto 1d57e17
Update README.md
yusinto 431dc65
fix: Replace common types with node server types.
yusinto 4419ec0
chore: Added login input and improved button styles.
yusinto d5bbc27
chore: Add server variation call for analytics.
yusinto 78a6810
chore: Added warning about directly calling ldClient.variation. Added…
yusinto a5abddc
Update helloClientComponent.tsx
yusinto 8b9ff98
chore: Rolled back client component. Moved identify example to its ow…
yusinto b88fa95
fix: Check for global variable in server variation.
yusinto e319133
chore: Added minor comment.
yusinto a383441
Update LDProvider.tsx
yusinto ac67a7e
chore: Added clientSideID to LDProvider.
yusinto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module.exports = { | ||
| extends: ['plugin:@next/next/recommended'], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Example only - Do not commit | ||
|
|
||
| LD_SDK_KEY='<YOUR LD SERVER SDK KEY>' | ||
| NEXT_PUBLIC_LD_CLIENT_SIDE_ID='<YOUR LD CLIENT SDK KEY>' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
|
||
| # dependencies | ||
| /node_modules | ||
| /.pnp | ||
| .pnp.js | ||
| .yarn/install-state.gz | ||
|
|
||
| # testing | ||
| /coverage | ||
|
|
||
| # next.js | ||
| /.next/ | ||
| /out/ | ||
|
|
||
| # production | ||
| /build | ||
|
|
||
| # misc | ||
| .DS_Store | ||
| *.pem | ||
|
|
||
| # debug | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| # local env files | ||
| .env*.local | ||
| .env | ||
|
|
||
| # vercel | ||
| .vercel | ||
|
|
||
| # typescript | ||
| *.tsbuildinfo | ||
| next-env.d.ts | ||
| .idea |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,35 @@ | ||
| # LaunchDarkly Universal SDK example | ||
|
|
||
| > [!IMPORTANT] | ||
| > This is an experimental project to demonstrate the use of LaunchDarkly with Next.js App Router. | ||
| > | ||
| > This is designed for the App Router. Pages router is not supported. | ||
|
|
||
| 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. | ||
| This example app uses the LaunchDarkly React Universal SDK. It features: | ||
|
|
||
| ## Quickstart | ||
| - Server side rendering with both Server Components and Client Components. | ||
| - A Client Component example in [app/components/helloClientComponent.tsx](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/react-universal/example/app/components/helloClientComponent.tsx) | ||
| - A Server Component (RSC) example in [app/components/helloServerComponent.tsx](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/react-universal/example/app/components/helloServerComponent.tsx) | ||
| - Out of the box bootstrapping. | ||
|
|
||
| To run this project: | ||
| This is a [Next.js](https://nextjs.org/) project created with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) using App Router. | ||
|
|
||
| ## Quickstart | ||
|
|
||
| 1. Create an .env file at repo root. | ||
| 2. Add your SDK key and client-side ID: | ||
| 1. Rename `.example.env.local` to `.env.local` and use your LaunchDarkly SDK keys: | ||
|
|
||
| ```dotenv | ||
| LD_SDK_KEY=sdk-*** | ||
| NEXT_PUBLIC_LD_CLIENT_SIDE_ID=*** | ||
| LD_SDK_KEY='<YOUR LD SERVER SDK KEY>' | ||
| NEXT_PUBLIC_LD_CLIENT_SIDE_ID='<YOUR LD CLIENT SDK KEY>' | ||
| ``` | ||
|
|
||
| 3. Replace `dev-test-flag` with your own flags in `app.tsx` and `LDButton.tsx`. | ||
| 4. `yarn && yarn dev` | ||
| 2. Either create `my-boolean-flag-1` in your LaunchDarkly environment or replace with your own flag in [helloClientComponent.tsx](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/react-universal/example/app/components/helloClientComponent.tsx) and [helloServerComponent.tsx](https://github.com/launchdarkly/js-core/tree/main/packages/sdk/react-universal/example/app/components/helloServerComponent.tsx). | ||
|
|
||
| 3. Finally: | ||
|
|
||
| ```bash | ||
| npm i && npm run dev | ||
|
|
||
| # or | ||
| yarn && yarn dev | ||
| ``` | ||
|
|
||
| You should see your flag value rendered in the browser. | ||
| You will see both Server and Client Components are rendered on the server (view source on your browser). However, only Client Components will respond to live changes because Server Components are excluded from the client bundle. |
25 changes: 25 additions & 0 deletions
25
packages/sdk/react-universal/example/app/components/helloClientComponent.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 'use client'; | ||
|
|
||
| import { useLDClient } from '@launchdarkly/react-universal-sdk/client'; | ||
|
|
||
| export default function HelloClientComponent() { | ||
| const ldc = useLDClient(); | ||
|
|
||
| // WARNING: Using the ldClient to evaluate flags directly like this in prod | ||
| // can result in high event volumes. This example is contrived and is meant for | ||
| // demo purposes only. The recommended way is to utilise the `useVariation` hooks | ||
| // which should be supported soon. | ||
| const flagValue = ldc.variation('my-boolean-flag-1'); | ||
kinyoklion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <div className="border-2 border-white/20 p-4 "> | ||
| <div> | ||
| <p className="ldgradient text-xl"> | ||
| {flagValue | ||
| ? 'This flag is evaluating True running Client-Side JavaScript' | ||
| : 'This flag is evaluating False running Client-Side JavaScript'} | ||
| </p> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
packages/sdk/react-universal/example/app/components/helloIdentify.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 'use client'; | ||
|
|
||
| import { useState } from 'react'; | ||
| import { useCookies } from 'react-cookie'; | ||
|
|
||
| import type { JSSdk } from '@launchdarkly/react-universal-sdk'; | ||
| import { useLDClient } from '@launchdarkly/react-universal-sdk/client'; | ||
|
|
||
| export default function HelloIdentify() { | ||
| const ldc = useLDClient(); | ||
| const [_, setCookie] = useCookies(['ld']); | ||
| const [contextKey, setContextKey] = useState(''); | ||
|
|
||
| // WARNING: Using the ldClient to evaluate flags directly like this in prod | ||
| // can result in high event volumes. This example is contrived and is meant for | ||
| // demo purposes only. The recommended way is to utilise the `useVariation` hooks | ||
| // which should be supported soon. | ||
| const flagValue = ldc.variation('my-boolean-flag-1'); | ||
|
|
||
| function onClickLogin() { | ||
| const context = { kind: 'user', key: contextKey }; | ||
| (ldc as JSSdk).identify(context).then(() => { | ||
| console.log('identify successful, persisting to cookies'); | ||
| setCookie('ld', context); | ||
| }); | ||
| } | ||
|
|
||
| return ( | ||
| <div className="border-2 border-white/20 p-4 "> | ||
| <div> | ||
| <p className="ldgradient text-xl"> | ||
| {flagValue | ||
| ? 'This flag is evaluating True running Client-Side JavaScript' | ||
| : 'This flag is evaluating False running Client-Side JavaScript'} | ||
| </p> | ||
| </div> | ||
| <br /> | ||
| <div> | ||
| <label> | ||
| Context key: | ||
| <input value={contextKey} onChange={(e) => setContextKey(e.target.value)} /> | ||
| </label> | ||
| </div> | ||
| <br /> | ||
| <button role="button" className="login" onClick={onClickLogin}> | ||
| Login | ||
| </button> | ||
| </div> | ||
| ); | ||
| } |
18 changes: 18 additions & 0 deletions
18
packages/sdk/react-universal/example/app/components/helloServerComponent.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { getLDContext } from '@/app/utils'; | ||
|
|
||
| import { useLDClientRsc } from '@launchdarkly/react-universal-sdk/server'; | ||
|
|
||
| export default async function HelloServerComponent() { | ||
| const ldc = await useLDClientRsc(getLDContext()); | ||
| const flagValue = ldc.variation('my-boolean-flag-1'); | ||
|
|
||
| return ( | ||
| <div className="border-2 border-white/20 p-4"> | ||
| <p className="text-xl ldgradient"> | ||
| {flagValue | ||
| ? 'This flag is evaluating True in a React Server Component' | ||
| : 'This flag is evaluating False in a React Server Component'} | ||
| </p> | ||
| </div> | ||
| ); | ||
| } |
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This universal sdk should work with all framework which supports rsc (gatsby, remix, etc).