How to Add a Chatbot to a React, Next.js or Vue App

A website chatbot is not a React or Vue component. It is a small script that adds its own button to the page, outside your app. In a single-page app that changes two things: where you load it, and how often. This guide shows the right place in React, Next.js, Vue/Nuxt and Angular, how to wait for cookie consent, and a Content Security Policy we tested.

The examples use the official HolderChat snippet. The same ideas apply to most script-based chat widgets. If your site is not a JavaScript app, the shorter 5-minute embed guide is enough. On WordPress, use the HolderChat Connector plugin instead.

Mint illustration: index.html, app/layout.tsx, nuxt.config.ts and src/index.html all lead to one script tag, which adds a chat button to a web page protected by a security shield
One script tag, loaded once from your app shell, puts the chat button on every route.

Before you start

  • A chatbot in the HolderChat dashboard with status Active.
  • Its snippet from Widget Appearance → Embed on your website (or the Get Widget Code screen). The token in the URL belongs to that one bot.
  • A choice between the two official embed methods:
MethodSnippetChoose it when
Script embed (standard)/dynamic/get.js?token=…You want the simplest setup. The widget lives directly on your page.
Iframe embed/dynamic/iframe.js?token=…You have a strict CSP, want the widget isolated from your page, or need custom CSS/JS for the widget.

The script snippet, exactly as HolderChat gives it to you:

<script src="https://holderchat.com/dynamic/get.js?token=YOUR_TOKEN" async></script>

Details on both methods are in Embedding the Chatbot Widget.

Building a docs or help bot for a SaaS product? AI chatbot for SaaS covers which sources to add and how to route sales and support.

Three facts about the loader that matter in an SPA

We read the current loader (get.js) and tested it on a local page on 23 November 2026. Three facts decide how you add it:

  1. It needs a real script tag with that URL. The loader finds its token by looking for a <script> whose src contains /dynamic/get.js. Add it as a script element: use next/script, document.createElement("script"), or the HTML file. Do not bundle it, import it, or fetch it and eval it.
  2. It runs once per page load. The loader sets a global flag, so a second copy does nothing. Injecting it after the page has loaded works fine; we tested a delayed, client-side injection.
  3. Unmounting does not reset it. window.holderchatCleanup() removes the widget, but the flag stays. The widget does not come back until the next full page load. So load it once, high in the app, and do not add and remove it per route.

One more detail: if your page does not already have jQuery and DOMPurify, the loader fetches them from code.jquery.com and cdnjs.cloudflare.com with integrity hashes. If your app already exposes window.jQuery and window.DOMPurify, it uses those instead. This matters for the CSP below.

React (Vite or Create React App)

Option A: the HTML shell (simplest)

Every React SPA still has one real HTML file. Paste the snippet before </body>. It loads with the shell and stays when React Router changes the route.

<!-- index.html (Vite: project root; Create React App: public/index.html) -->
<body>
  <div id="root"></div>
  <script type="module" src="/src/main.tsx"></script>
  <script src="https://holderchat.com/dynamic/get.js?token=YOUR_TOKEN" async></script>
</body>

Option B: a small component

Use this if the token should come from an environment variable, or if you want the widget only in production builds. Render it once, outside your routes.

// src/HolderChat.jsx
import { useEffect } from "react";

const TOKEN = import.meta.env.VITE_HOLDERCHAT_TOKEN; // CRA: process.env.REACT_APP_HOLDERCHAT_TOKEN

export default function HolderChat() {
  useEffect(() => {
    if (!TOKEN) return; // no token (e.g. local dev): no widget
    if (document.querySelector('script[src*="/dynamic/get.js"]')) return; // already added
    const s = document.createElement("script");
    s.src = "https://holderchat.com/dynamic/get.js?token=" + encodeURIComponent(TOKEN);
    s.async = true;
    document.body.appendChild(s);
    // No cleanup on purpose: the widget belongs to the page, not to this component.
  }, []);
  return null;
}

// src/App.jsx: render it once, outside your routes
// <BrowserRouter> ... </BrowserRouter>
// <HolderChat />

In development, React StrictMode runs effects twice. The querySelector check stops a second tag, and the loader’s own flag would also ignore it. The token is not a secret; it is visible in the public snippet anyway. An env variable just keeps it out of your code and lets you switch it off locally.

Next.js

App Router

Put next/script in the root layout. Next.js loads it once and keeps it across client-side navigation. lazyOnload waits until the browser is idle, which suits a chat button. Use afterInteractive if you want the button to appear sooner.

// app/layout.tsx
import Script from "next/script";

const token = process.env.NEXT_PUBLIC_HOLDERCHAT_TOKEN;

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        {token && (
          <Script
            src={`https://holderchat.com/dynamic/get.js?token=${token}`}
            strategy="lazyOnload"
          />
        )}
      </body>
    </html>
  );
}

Pages Router

// pages/_app.tsx
import type { AppProps } from "next/app";
import Script from "next/script";

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Component {...pageProps} />
      <Script
        src="https://holderchat.com/dynamic/get.js?token=YOUR_TOKEN"
        strategy="lazyOnload"
      />
    </>
  );
}

Do not place the Script in a single page component unless you only want chat there. Because of the run-once behaviour above, the widget then stays after the visitor navigates away, until the next full reload.

Vue, Nuxt and Angular

Vue 3 with Vite and Angular work like plain React. Paste the snippet before </body> in index.html (Vue: project root; Angular: src/index.html). If you need an environment variable, use the same createElement pattern from Option B in main.ts or your root component’s onMounted/ngOnInit.

Nuxt 3 has no editable HTML file. Add the script globally in the config:

// nuxt.config.ts (Nuxt 3)
export default defineNuxtConfig({
  app: {
    head: {
      script: [
        {
          src: "https://holderchat.com/dynamic/get.js?token=YOUR_TOKEN",
          async: true,
          tagPosition: "bodyClose",
        },
      ],
    },
  },
});

Wait for cookie consent if you need to

The script embed sets two cookies for session continuity: holderchat_user_id and holderchat_talk_id. Both last 48 hours (see the embed documentation). If your consent banner controls third-party scripts, do not hard-code the tag. Load it from the banner’s “accepted” callback:

// Load the widget only after the visitor has agreed
function loadHolderChat(token) {
  if (document.querySelector('script[src*="/dynamic/get.js"]')) return;
  const s = document.createElement("script");
  s.src = "https://holderchat.com/dynamic/get.js?token=" + encodeURIComponent(token);
  s.async = true;
  document.body.appendChild(s);
}

// Call this from your consent tool's "accepted" callback:
// loadHolderChat("YOUR_TOKEN");

Whether you need consent for this depends on your setup and jurisdiction. This page shows the technique; it is not legal advice.

Content Security Policy: tested settings

Many SPAs send a CSP header. With a strict policy, the browser blocks the widget and only the console shows why. We loaded both embeds on a local test page in headless Chromium, with a CSP header, on 23 November 2026. These are the directives that made the launcher load and the chat connect without CSP errors. Merge them into your existing policy; do not replace it.

Script embed

Content-Security-Policy:
  script-src  'self' 'unsafe-inline' https://holderchat.com https://code.jquery.com https://cdnjs.cloudflare.com;
  style-src   'self' 'unsafe-inline' https://holderchat.com;
  font-src    'self' https://holderchat.com data:;
  img-src     'self' https://holderchat.com data:;
  media-src   https://holderchat.com;
  connect-src 'self' https://holderchat.com wss://holderchat.com;

Without 'unsafe-inline', Chromium reported blocked inline styles, a blocked inline script (the loader adds the widget’s styles and a small configuration script to your page) and a blocked font from holderchat.com. Without the two CDN hosts, the loader cannot load jQuery and DOMPurify, and no widget appears.

Iframe embed (works with a strict policy)

<script src="https://holderchat.com/dynamic/iframe.js?token=YOUR_TOKEN" async></script>
Content-Security-Policy:
  script-src  'self' https://holderchat.com;
  connect-src 'self' https://holderchat.com;
  frame-src   https://holderchat.com;

No 'unsafe-inline' needed. The chat runs inside a sandboxed frame on holderchat.com, so its styles, fonts and WebSocket fall under that frame, not your page. Trade-offs, as listed in the docs: a click outside the chat does not close it (use the close control), and the conversation id is kept in your page’s localStorage. If your policy uses nonces or hashes, this is the method to use. Custom CSS and JavaScript also work only with this embed; see Custom CSS and JavaScript (iframe only).

If the console still shows a CSP violation, it names the blocked URL and directive. Add that exact source and reload.

Check it before you ship

  • Open the production build in a private window with ad blockers off. The button should appear in the bottom corner.
  • Navigate between three or four routes. There should be one button, not two, and an open conversation should stay.
  • In DevTools → Elements, search for /dynamic/get.js. There should be one match.
  • Check the Console for CSP or 4xx errors, then send a test question.
  • Before going live, run through Test Your Website Chatbot Before You Go Live.

FAQ

Is there an npm package or a React component for HolderChat?

No. The official ways are the script embed and the iframe embed shown above. The small component in this guide just adds the official script tag once.

Can I show the chat on only some routes?

You can load it from one page or layout, so it only appears once a visitor reaches that section. It then stays until the next full page load, because the loader runs once per page load. For a site-wide widget, load it in the root layout or HTML shell.

Does the widget slow down my app?

The tag is async and runs outside your component tree, so it does not block rendering or hydration. In Next.js, lazyOnload moves it to browser idle time. Check your own numbers with Lighthouse or PageSpeed Insights before and after.

Does it work on localhost?

In our test the widget loaded on a page served from localhost. Remember that every conversation you start goes to your real bot’s history.

Ready to add it? Create a bot, add your pages or documents, copy the snippet from Widget Appearance, and use the pattern above for your framework. Questions about a setup that is not covered here? Ask the chat on this page.