Overview
A complete Vite + React single-page app. It creates one browser client with a client SDK key, readsnew-checkout and search-variant with typed hooks, and calls identify after sign-in.
What’s in it
src/lib/shipd.tscreates the browser client fromimport.meta.env.VITE_SHIPD_SDK_KEYand binds typed hooks withcreateShipdReact<FlagDefinitions>().src/main.tsxwraps the app inShipdProvider.src/App.tsxsigns a user in and out. It callsidentifyon sign-in andreseton sign-out, and the hooks re-render.- The browser client caches the last good snapshot and a stable anonymous id in
localStorage, so repeat visits render flags before the network responds.
Files
package.json
tsconfig.json
vite.config.ts
index.html
shipd.config.ts
.env.example
src/vite-env.d.ts
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.ts
src/main.tsx
signIn with your auth library. The important part is calling identify with the user’s id and traits once you know them.
src/App.tsx
src/components/checkout-button.tsx
src/components/search-box.tsx
Run it
http://localhost:5173. Pushed flags serve their default. In the dashboard, add a rule to new-checkout that serves true when plan equals pro, then click Save environment. Click Sign in and the checkout link switches without a reload.
Adapt it
- Call
identifywherever your auth state settles (session restore, sign-in callback). Callreseton sign-out. - Pass per-component context only for values that aren’t about the user, such as
useFlag("new-checkout", { attributes: { cartSize: 3 } }). Per-call attributes overrideidentifytraits. - The browser client adds
country,region,city, andtimezoneattributes automatically. PassautomaticTraits: falseto turn that off. - Gate UI with
useReady()only where a flash of the default matters. ThelocalStoragecache makes repeat visits ready almost immediately.