> ## Documentation Index
> Fetch the complete documentation index at: https://confidence.spotify.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Next.js

> Confidence SDK for feature flag evaluation in Next.js applications with App Router support.

The Confidence Next.js SDK provides feature flag evaluation for Next.js applications using the [Confidence Resolver](https://github.com/spotify/confidence-resolver)—a Rust-based resolver that runs as WebAssembly. It supports both React Server Components and Client Components with the App Router.

## Features

* **Local evaluation**: Flag rules evaluate on your infrastructure in microseconds
* **App Router support**: Works with React Server Components and Client Components
* **OpenFeature compatible**: Standard feature flag API through OpenFeature provider
* **Background sync**: Flag rules and logging sync with Confidence in the background
* **High reliability**: No network dependency at evaluation time

## Installation

<CodeGroup>
  ```bash npm theme={null}
  npm install @spotify-confidence/openfeature-server-provider-local
  ```

  ```bash yarn theme={null}
  yarn add @spotify-confidence/openfeature-server-provider-local
  ```

  ```bash pnpm theme={null}
  pnpm add @spotify-confidence/openfeature-server-provider-local
  ```
</CodeGroup>

**Requirements**: Node.js 18+ with WebAssembly support.

## Setup

### 1. Configure the provider

Create a file to initialize the Confidence provider:

```typescript theme={null}
// lib/confidence.ts
import { OpenFeature } from '@openfeature/server-sdk';
import { createConfidenceServerProvider } from '@spotify-confidence/openfeature-server-provider-local';

const provider = createConfidenceServerProvider({
  flagClientSecret: process.env.CONFIDENCE_FLAG_CLIENT_SECRET!,
});

// Initialize provider at startup
await OpenFeature.setProviderAndWait(provider);
```

### 2. Add the provider to your layout

Wrap your application with the `ConfidenceProvider`:

```tsx theme={null}
// app/layout.tsx
import { ConfidenceProvider } from '@spotify-confidence/openfeature-server-provider-local/react-server';
import './lib/confidence';

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  const context = {
    targetingKey: 'user-123',
    country: 'US',
  };

  return (
    <html>
      <body>
        <ConfidenceProvider context={context}>{children}</ConfidenceProvider>
      </body>
    </html>
  );
}
```

## Usage

### Server Components

Use the `getFlag` function in React Server Components:

```tsx theme={null}
// app/page.tsx
import { getFlag } from '@spotify-confidence/openfeature-server-provider-local/react-server';

export default async function Page() {
  const showNewLayout = await getFlag('page-layout.showNewLayout', false, {
    targetingKey: 'user-123'
  });

  return showNewLayout ? <NewLayout /> : <OldLayout />;
}
```

### Client Components

Use the `useFlag` hook in Client Components:

```tsx theme={null}
// components/FeatureButton.tsx
'use client';
import { useFlag } from '@spotify-confidence/openfeature-server-provider-local/react-client';

export function FeatureButton() {
  const enabled = useFlag('new-feature.enabled', false);

  if (!enabled) return null;

  return <button>New Feature</button>;
}
```

## Resources

<CardGroup cols={2}>
  <Card title="GitHub repository" icon="github" href="https://github.com/spotify/confidence-resolver/tree/main/openfeature-provider/js">
    Source code and examples
  </Card>

  <Card title="OpenFeature JS SDK" icon="flag" href="https://openfeature.dev/docs/reference/technologies/server/javascript">
    OpenFeature documentation
  </Card>

  <Card title="Context" icon="sliders" href="/docs/sdks/context">
    Configure evaluation context
  </Card>

  <Card title="Apply events" icon="chart-line" href="/docs/sdks/apply-event">
    Understand flag assignment tracking
  </Card>
</CardGroup>
