Skip to content

Commit d0fb7da

Browse files
committed
docs: add app router section
1 parent 29e774e commit d0fb7da

1 file changed

Lines changed: 106 additions & 1 deletion

File tree

packages/react-sdk/README.md

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ app.get("/bootstrap", (req, res) => {
250250
});
251251
```
252252

253-
### Next.js SSR example
253+
### Next.js Page Router SSR example
254254

255255
For Next.js applications using server-side rendering, you can pre-fetch flags in `getServerSideProps`:
256256

@@ -311,6 +311,111 @@ function HuddleFeature() {
311311

312312
This approach eliminates loading states and improves performance by avoiding the initial flags API call.
313313

314+
### Next.js App Router example
315+
316+
For Next.js applications using the App Router (Next.js 13+), you can pre-fetch flags in Server Components and pass them to client components:
317+
318+
```typescript
319+
// app/layout.tsx (Server Component)
320+
import { ReflagClient as ReflagNodeClient } from "@reflag/node-sdk";
321+
import { ClientProviders } from "./providers";
322+
323+
async function getBootstrapData() {
324+
const serverClient = new ReflagNodeClient({
325+
secretKey: process.env.REFLAG_SECRET_KEY!
326+
});
327+
await serverClient.initialize();
328+
329+
// In a real app, you'd get user/company from your auth system
330+
const bootstrapData = serverClient.getFlagsForBootstrap({
331+
user: { id: "user123", name: "John Doe", email: "john@acme.com" },
332+
company: { id: "company456", name: "Acme Inc", plan: "enterprise" },
333+
other: { source: "web" }
334+
});
335+
336+
return bootstrapData;
337+
}
338+
339+
export default async function RootLayout({
340+
children,
341+
}: {
342+
children: React.ReactNode;
343+
}) {
344+
const bootstrapData = await getBootstrapData();
345+
346+
return (
347+
<html lang="en">
348+
<body>
349+
<ClientProviders bootstrapData={bootstrapData}>
350+
{children}
351+
</ClientProviders>
352+
</body>
353+
</html>
354+
);
355+
}
356+
```
357+
358+
```typescript
359+
// app/providers.tsx (Client Component)
360+
"use client";
361+
362+
import { ReflagBootstrappedProvider, BootstrappedFlags } from "@reflag/react-sdk";
363+
364+
interface ClientProvidersProps {
365+
children: React.ReactNode;
366+
bootstrapData: BootstrappedFlags;
367+
}
368+
369+
export function ClientProviders({ children, bootstrapData }: ClientProvidersProps) {
370+
return (
371+
<ReflagBootstrappedProvider
372+
publishableKey={process.env.NEXT_PUBLIC_REFLAG_PUBLISHABLE_KEY!}
373+
flags={bootstrapData}
374+
>
375+
{children}
376+
</ReflagBootstrappedProvider>
377+
);
378+
}
379+
```
380+
381+
```typescript
382+
// app/page.tsx (Server Component)
383+
import { HuddleFeature } from "./huddle-feature";
384+
385+
export default function HomePage() {
386+
return (
387+
<main>
388+
<h1>My App</h1>
389+
<HuddleFeature />
390+
</main>
391+
);
392+
}
393+
```
394+
395+
```typescript
396+
// app/huddle-feature.tsx (Client Component)
397+
"use client";
398+
399+
import { useFlag } from "@reflag/react-sdk";
400+
401+
export function HuddleFeature() {
402+
const { isEnabled, track, config } = useFlag("huddle");
403+
404+
if (!isEnabled) return null;
405+
406+
return (
407+
<div>
408+
<h2>Start a Huddle</h2>
409+
<p>Max participants: {config.payload?.maxParticipants ?? 10}</p>
410+
<p>Video quality: {config.payload?.videoQuality ?? "standard"}</p>
411+
<button onClick={track}>Start Huddle</button>
412+
</div>
413+
);
414+
}
415+
```
416+
417+
This App Router approach leverages Server Components for server-side flag fetching while using Client Components only where React state and hooks are needed.
418+
314419
## `<ReflagProvider>` component
315420

316421
The `<ReflagProvider>` initializes the Reflag SDK, fetches flags and starts listening for automated feedback survey events. The component can be configured using a number of props:

0 commit comments

Comments
 (0)