Overview
A complete Next.js App Router app. A server component evaluatesnew-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.tsholds one server client per process.import "server-only"keeps the server key out of the browser bundle.src/lib/shipd-react.tsbinds typed hooks once withcreateShipdReact<FlagDefinitions>().src/app/providers.tsxcreates the browser client withNEXT_PUBLIC_SHIPD_SDK_KEYand callsidentifyfor the signed-in user.src/app/page.tsxevaluatesnew-checkouton the server.src/components/search-box.tsxevaluatessearch-variantin 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
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
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 callidentifyon 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, andtimezoneattributes 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
shipdfrom@/lib/shipd-serverin the same way.