You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create a fileto initialize the OpenFeature provider:
40
+
Use Next.js's [`instrumentation.ts`](https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation) hook to register the provider once at server startup:
`register` runs once when a Next.js server instance boots and is awaited before requests are served. It does not run during `next build`, so it's safe to perform network setup here.
exportdefaultasyncfunction RootLayout({ children }: { children:React.ReactNode }) {
65
68
// Get user context from session, cookies, etc.
@@ -387,6 +390,98 @@ export default async function Page() {
387
390
}
388
391
```
389
392
393
+
## Next.js Pages Router
394
+
395
+
When using the Pages Router, three subexports under `./pages-router/*` cover the equivalent of the App Router integration above. The client hooks (`useFlag`, `useFlagDetails`) are the same — only the server plumbing differs.
396
+
397
+
-**`pages-router/server`** — `withConfidence`, a `getServerSideProps` decorator that resolves the flag bundle for the request and merges it into `pageProps`.
398
+
-**`pages-router/client`** — `<ConfidencePagesProvider>`, which reads the bundle from `pageProps` and exposes it to the `useFlag` / `useFlagDetails` hooks (re-used as-is from `react-client`).
399
+
-**`pages-router/api`** — `applyHandler`, the `/api/confidence/apply` POST handler the client uses to log exposure when a flag is read.
400
+
401
+
Provider registration is router-agnostic: use the same [`instrumentation.ts`](#1-set-up-the-provider-server-side) setup shown for the App Router.
402
+
403
+
### 1. Resolve flags in `getServerSideProps`
404
+
405
+
`withConfidence` wraps a single `getServerSideProps`-shaped function that does your data fetching **and** returns the evaluation context to use for flag resolution (and optionally a `flags` allow-list). The decorator resolves the bundle without firing exposure, seals the resolve token, and merges it into `pageProps.confidence`.
Returning `{ redirect }` or `{ notFound }` short-circuits before flag resolution, just like a normal `getServerSideProps`. For pages without their own data fetching, the body collapses to a single return:
### 2. Wrap your tree with `<ConfidencePagesProvider>`
438
+
439
+
Any component that calls `useFlag` / `useFlagDetails` must sit under a `<ConfidencePagesProvider>`. The simplest placement is `_app.tsx`, which covers every page in one spot — but the wrapper works anywhere in the tree, so per-page wrapping is also fine if you'd rather not touch `_app.tsx`.
Pages whose `getServerSideProps` doesn't use `withConfidence` simply have no `confidence` key on `pageProps`; the wrapper short-circuits and any `useFlag` calls in their tree fall back to default values.
457
+
458
+
### 3. Mount the apply API route
459
+
460
+
When a flag is read on the client (e.g. `useFlag` firing on mount), the wrapper POSTs `{ resolveToken, flagName }` to the apply route, which opens the sealed token and logs exposure server-side. Mount the handler at `/api/confidence/apply`:
If you need to mount it elsewhere, pass the same path to `<ConfidencePagesProvider apiPath="...">`.
469
+
470
+
### Resolve token security
471
+
472
+
In the App Router, the resolve token stays in the encrypted closure of a server action. The Pages Router has no equivalent, so the lib **seals the token with AES-256-GCM** before it ever reaches the browser. Set a server-only key:
473
+
474
+
```bash
475
+
CONFIDENCE_TOKEN_KEY=$(openssl rand -hex 32)
476
+
```
477
+
478
+
The client only ever sees the sealed value; the apply API route opens it server-side.
479
+
480
+
### What's not in this integration
481
+
482
+
- No equivalent to `getFlag` / `getFlagDetails` server functions — call `OpenFeature.getClient().getXxxValue(...)` directly inside `getServerSideProps` if you need eager-exposure server-only resolution.
483
+
- No `getStaticProps` support: flag resolution is per-request and depends on evaluation context.
484
+
390
485
## Troubleshooting
391
486
392
487
### "ConfidenceProvider requires a ConfidenceServerProviderLocal"
0 commit comments