-
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.
Update Next 14 examples for the upcoming SDK v1.0.0 release
- Loading branch information
Showing
12 changed files
with
235 additions
and
208 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
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,9 +1,25 @@ | ||
import { getInstance, getPayload } from "@/lib/growthbookServer"; | ||
import { configureServerSideGrowthBook } from "@/lib/growthbookServer"; | ||
import ClientApp from "./ClientApp"; | ||
import { GrowthBook } from "@growthbook/growthbook"; | ||
|
||
export default async function PrerenderedClientPage() { | ||
// Get server-side GrowthBook instance in order to fetch the feature flag payload | ||
const gb = await getInstance(); | ||
// Helper to configure cache for next.js | ||
configureServerSideGrowthBook(); | ||
|
||
return <ClientApp payload={getPayload(gb)} />; | ||
// Create and initialize a GrowthBook instance | ||
const gb = 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, | ||
}); | ||
await gb.init({ timeout: 1000 }); | ||
|
||
// Get the payload to hydrate the client-side GrowthBook instance | ||
// We need the decrypted payload so the initial client-render can be synchronous | ||
const payload = gb.getDecryptedPayload(); | ||
|
||
// Cleanup your GrowthBook instance | ||
gb.destroy(); | ||
|
||
return <ClientApp payload={payload} />; | ||
} |
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 |
---|---|---|
@@ -1,39 +1,33 @@ | ||
"use client"; | ||
import Cookies from "js-cookie"; | ||
import { AutoExperiment, FeatureDefinition } from "@growthbook/growthbook"; | ||
import { gb, setPayload } from "@/lib/growthbookClient"; | ||
import { GrowthBookProvider } from "@growthbook/growthbook-react"; | ||
import ClientComponent from "./ClientComponent"; | ||
import { onExperimentView } from "@/lib/GrowthBookTracking"; | ||
import { GrowthBookPayload } from "@growthbook/growthbook"; | ||
import { GrowthBook, GrowthBookProvider } from "@growthbook/growthbook-react"; | ||
import { PropsWithChildren, useMemo } from "react"; | ||
import { GB_UUID_COOKIE } from "@/middleware"; | ||
import { useCallback, useEffect, useRef } from "react"; | ||
import Cookies from "js-cookie"; | ||
|
||
export default function ClientApp({ | ||
payload, | ||
}: { | ||
payload: { | ||
features: Record<string, FeatureDefinition<unknown>>; | ||
experiments: AutoExperiment[]; | ||
}; | ||
}) { | ||
// Helper to hydrate client-side GrowthBook instance with payload from the server | ||
const hydrate = useCallback(() => { | ||
setPayload(payload); | ||
gb.setAttributes({ | ||
id: Cookies.get(GB_UUID_COOKIE), | ||
}); | ||
}, [payload]); | ||
|
||
// Hydrate immediately on first render and whenever the payload changes | ||
const ref = useRef<boolean>(); | ||
if (!ref.current) { | ||
ref.current = true; | ||
hydrate(); | ||
} | ||
useEffect(() => hydrate(), [hydrate]); | ||
|
||
return ( | ||
<GrowthBookProvider growthbook={gb}> | ||
<ClientComponent /> | ||
</GrowthBookProvider> | ||
children, | ||
}: PropsWithChildren<{ payload: GrowthBookPayload }>) { | ||
// Create a singleton GrowthBook instance for this page | ||
const gb = useMemo( | ||
() => | ||
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, | ||
trackingCallback: onExperimentView, | ||
attributes: { | ||
id: Cookies.get(GB_UUID_COOKIE), | ||
}, | ||
}).initSync({ | ||
payload, | ||
// Optional, enable streaming updates | ||
streaming: true, | ||
}), | ||
[payload] | ||
); | ||
|
||
return <GrowthBookProvider growthbook={gb}>{children}</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
Oops, something went wrong.