Installation
What the widget does on your pages: the scripts it loads, the calls it makes, what it stores and when it uses the microphone.
Install
npm install @monoverse/next
next (14, 15 or 16), react and react-dom (18 or 19) are peer dependencies. The package is
tested on Next 14 with React 18, Next 15 with React 19 and Next 16 with React 19.
Render in the root layout (App Router)
Render <MonoverseWidget> once in your root layout so it persists across client navigations
and never re-injects per route. The component is 'use client'; importing it into a Server Component
layout is fine.
import { MonoverseWidget } from '@monoverse/next';
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="uk">
<body>
{children}
<MonoverseWidget publicKey="pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" />
</body>
</html>
);
}
It renders nothing visible — the widget UI is mounted by the deployed bundle it loads into its own
Shadow DOM host. The widget core is a one-per-page singleton; next/script caches the tag by
src, so a re-render never opens a second WebSocket or microphone.
Voice or chat
- Voice
- Chat
<MonoverseWidget publicKey="pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" variant="voice" />
variant="voice" (the default) loads widget-voice.js.
<MonoverseWidget publicKey="pk_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" variant="chat" />
variant="chat" loads widget-chat.js.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
publicKey | string (required) | — | Your pk_ publishable key. |
variant | 'voice' | 'chat' | 'voice' | Which widget bundle to load. |
lang | string | — | Force the widget UI language (e.g. 'uk', 'en'). Omit to let the backend pick. |
consent | 'granted' | 'denied' | — | Consent signal from your CMP — emitted as data-consent on the script tag. See Consent contract. |
scriptSrc | string | https://api.monoverse.tech/widget | Override the CDN base (self-hosted API). |
onError | (error: Error) => void | — | Called on script load failure or an empty key. |
onNavigate | (target: MonoverseNavTarget) => void | — | Called on any bot navigation. Route it through your SPA router so the live session survives. |
onAction | MonoverseActionHandlers | — | Cart / variant / filter handlers — the acting tier. See Capabilities & host actions. |
capabilities | MonoverseCapability[] | — | Narrow the server-granted capability set for the current page (narrow-only; never widens it). |
toolHandlers | MonoverseToolHandlers | — | Low-level override for canonical action names. Most apps use onAction instead. |
The last three (onAction, capabilities, toolHandlers) and onNavigate are the acting tier —
they let the bot add to cart, pick a variant, filter, and navigate. See
Capabilities & host actions and
Custom site integration.
SSR safety
next/script with strategy="afterInteractive" loads only in the browser after hydration, so
the widget code (which touches window/document) never runs on the server — the component is
SSR-safe by construction. On an empty publicKey it renders nothing and calls onError.
Teardown caveat (why root-layout placement matters)
next/script caches the <script> by src and intentionally does not remove it on unmount.
When the component unmounts it tears down the widget instance (window.Monoverse?.destroy?.() —
closing the WebSocket and releasing the mic), but the cached tag stays. Because the script is cached,
re-mounting on a later route may not re-run the bundle.
- Place the widget once in the root layout (it never unmounts) — this is the recommended path.
- To switch the key at runtime, remount the host with a React
key=. - If you must mount it on specific routes with full re-init, use
@monoverse/react'suseEffectinjector instead, which removes the tag on cleanup.
Bare embed (no package)
If you'd rather not add a dependency, the underlying mechanism is a single <Script> — this is what
the wrapper writes for you:
'use client';
import Script from 'next/script';
interface MonoverseEmbedProps {
publicKey: string;
apiBase?: string;
variant?: 'voice' | 'chat';
consent?: 'granted' | 'denied';
}
export function MonoverseEmbed({
publicKey,
apiBase = 'https://api.monoverse.tech',
variant = 'voice',
consent,
}: MonoverseEmbedProps) {
return (
<Script
src={`${apiBase}/widget/widget-${variant}.js`}
strategy="afterInteractive"
data-public-key={publicKey}
data-api-base={apiBase}
{...(consent ? { 'data-consent': consent } : {})}
/>
);
}
The package source, props table, and changelog live in the package README on npm.