-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add server/client distinction; use growthbook-react
- Loading branch information
Showing
11 changed files
with
63 additions
and
55 deletions.
There are no files selected for viewing
This file contains 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,24 @@ | ||
"use client"; | ||
import { useFeatureIsOn, useFeatureValue } from "@growthbook/growthbook-react"; | ||
export default function ClientComponent() { | ||
const feature1Enabled = useFeatureIsOn("feature1"); | ||
const feature2Value = useFeatureValue("feature2", "fallback"); | ||
return ( | ||
<div> | ||
<div className="text-4xl my-4">Client Component</div> | ||
<p className="my-2"> | ||
This component renders client-side. The page initially delivered to the | ||
client will not have FF values loaded, which can result in a | ||
'flicker' when values are loaded asynchronously client-side. | ||
</p> | ||
<ul> | ||
<li> | ||
feature1: <strong>{feature1Enabled ? "ON" : "OFF"}</strong> | ||
</li> | ||
<li> | ||
feature2: <strong>{feature2Value}</strong> | ||
</li> | ||
</ul> | ||
</div> | ||
); | ||
} |
This file contains 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,54 +1,28 @@ | ||
"use client"; | ||
import { useEffect, useState } from "react"; | ||
import { useEffect } from "react"; | ||
import { GrowthBookProvider } from "@growthbook/growthbook-react"; | ||
import Cookies from "js-cookie"; | ||
import gb from "@/lib/growthbook"; | ||
import gb from "@/lib/growthbook/client"; | ||
import ClientComponent from "./ClientComponent"; | ||
|
||
export default function ClientPage() { | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
useEffect(() => { | ||
const load = async () => { | ||
await gb.loadFeatures({ timeout: 1000 }); | ||
|
||
const userId = Cookies.get("gb-next-example-userId"); | ||
|
||
gb.setAttributes({ | ||
id: userId, | ||
}); | ||
try { | ||
await gb.loadFeatures(); | ||
const userId = Cookies.get("gb-next-example-userId"); | ||
gb.setAttributes({ | ||
id: userId, | ||
}); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
}; | ||
try { | ||
setIsLoading(true); | ||
load(); | ||
} catch (e) { | ||
console.error(e); | ||
} finally { | ||
setIsLoading(false); | ||
} | ||
load(); | ||
}, []); | ||
|
||
const feature1Enabled = gb.isOn("feature1"); | ||
const feature2Value = gb.getFeatureValue("feature2", "fallback"); | ||
|
||
return ( | ||
<div> | ||
<div className="text-4xl my-4">Client Component</div> | ||
<p className="my-2"> | ||
This component renders client-side. The page initially delivered to the | ||
client will not have FF values loaded, which can result in a 'flicker' | ||
when values are loaded asynchronously client-side. | ||
</p> | ||
{isLoading ? ( | ||
<div className="text-2xl animate-bounce">Loading...</div> | ||
) : ( | ||
<ul> | ||
<li> | ||
feature1: <strong>{feature1Enabled ? "ON" : "OFF"}</strong> | ||
</li> | ||
<li> | ||
feature2: <strong>{feature2Value}</strong> | ||
</li> | ||
</ul> | ||
)} | ||
</div> | ||
<GrowthBookProvider growthbook={gb}> | ||
<ClientComponent /> | ||
</GrowthBookProvider> | ||
); | ||
} |
This file contains 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 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 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 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 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 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 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,11 @@ | ||
import { GrowthBook } from "@growthbook/growthbook"; | ||
|
||
const growthbook = new GrowthBook({ | ||
apiHost: process.env.NEXT_PUBLIC_GROWTHBOOK_API_HOST, | ||
clientKey: process.env.NEXT_PUBLIC_GROWTHBOOK_CLIENT_KEY, | ||
decryptionKey: process.env.NEXT_PUBLIC_GROWTHBOOK_DECRYPTION_KEY, | ||
// client-side features enabled | ||
subscribeToChanges: true, | ||
}); | ||
|
||
export default growthbook; |
File renamed without changes.
This file contains 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