next/script component. It de-duplicates the script across client-side navigations and gives you control over when it loads.
1
Add the script to the root layout
Place it once, in the root layout, so it loads for every route.
app/layout.tsx
strategy="afterInteractive" (the default) loads the script after hydration, which is right for the SDK. Don’t use beforeInteractive — the SDK isn’t needed before your app is interactive, and that strategy blocks other scripts.2
Add type declarations
Declare the global so you can use
window.CashbackHive without any. Copy the complete declaration file from the Types reference into your project, for example at types/cashback-hive.d.ts.Make sure the file is included by your tsconfig.json (the default "include": ["**/*.ts", "**/*.tsx"] covers it).3
Create a readiness hook
Components render during hydration, before
afterInteractive scripts run, so a simple useEffect listener usually works — but a component that mounts after a client-side navigation would wait forever for an event that already fired. useSyncExternalStore handles both cases cleanly and satisfies the React Compiler lint rules.hooks/use-cashback-hive.ts
4
Use it in a client component
components/offers.tsx
window, so anything that calls the SDK must be a Client Component ("use client"). You can still render the Client Component from a Server Component page.Environment variables
If you prefer not to hardcode the key, read it from aNEXT_PUBLIC_ variable. This is safe only for the publishable key.
Content Security Policy
If your app sets a CSP (vianext.config.js headers or middleware), add https://sdk.cashbackhive.com to script-src and connect-src. See Content Security Policy.