Skip to main content

Overview

A complete Next.js App Router app. A server component evaluates new-checkout with a server SDK key. A client component reads search-variant with typed React hooks and a client SDK key.

What’s in it

  • src/lib/shipd-server.ts holds one server client per process. import "server-only" keeps the server key out of the browser bundle.
  • src/lib/shipd-react.ts binds typed hooks once with createShipdReact<FlagDefinitions>().
  • src/app/providers.tsx creates the browser client with NEXT_PUBLIC_SHIPD_SDK_KEY and calls identify for the signed-in user.
  • src/app/page.tsx evaluates new-checkout on the server. src/components/search-box.tsx evaluates search-variant in the browser.
next.config.ts, tsconfig.json, and next-env.d.ts are the create-next-app defaults, including the @/* → ./src/* path alias.

Files

package.json
shipd.config.ts
.env.example
shipd push and shipd types gen write this file. Commit it and do not edit it by hand.
src/generated/flags.ts
src/lib/shipd-server.ts
src/lib/shipd-react.ts
Replace getUserId with your auth library. This example reads a uid cookie.
src/lib/session.ts
src/app/providers.tsx
src/app/layout.tsx
src/app/page.tsx
src/components/search-box.tsx

Run it

Open http://localhost:3000. To test targeting, set a uid cookie (for example usr_8f2k19) in your browser’s dev tools. Pushed flags serve their default. Add a rule for new-checkout in the dashboard, click Save environment, and reload.

Adapt it

  • Server components are rendered per request. Pass the evaluation context on every call ({ userId, attributes }). Don’t call identify on the shared server client.
  • Put the browser user’s traits in identify({ userId, traits: { plan } }). Hooks re-render when context or the snapshot changes.
  • The browser client adds country, region, city, and timezone attributes automatically. The server client does not. If a rule targets location, pass those attributes on the server yourself.
  • For route handlers and server actions, import shipd from @/lib/shipd-server in the same way.

Next