HomeIntegrationsNext.js & React Chatbot
Next.js App Router, Pages Router & React

AI Chatbot Integration for Next.js & React

Embed an under 20 KB vanilla JavaScript AI support agent using next/script with zero SSR hydration warnings. Keep app/layout.tsx as a Server Component while Knogents handles sitemap syncing, PDF and Excel retrieval, and email lead alerts.

Zero SSR hydration errorsServer Component compatibleFloating, inline, and fullscreen modes

Copy-Paste Setup for Next.js & React

Pick the snippet that matches your router and widget layout.

01 / APP ROUTER

Floating Widget in app/layout.tsx

Stays a Server Component

Add two next/script tags inside <body> using strategy="afterInteractive". The widget loads site-wide after hydration without adding 'use client' to your root layout.

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

export default function RootLayout({
  children,
}: {
  children: React.ReactNode;
}) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script id="knogents-config" strategy="afterInteractive">
          {`window.widgetConfig = {
            chatbotId: "YOUR_CHATBOT_ID"
          };`}
        </Script>
        <Script
          id="knogents-widget-script"
          data-id="YOUR_CHATBOT_ID"
          src="https://knogents.com/embed.js"
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}
02 / PAGES ROUTER

Global Floating Widget in pages/_app.tsx

Next.js Pages Router

If your app uses the Pages Router, place the same next/script tags directly inside pages/_app.tsx.

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

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Component {...pageProps} />
      <Script id="knogents-config" strategy="afterInteractive">
        {`window.widgetConfig = {
          chatbotId: "YOUR_CHATBOT_ID"
        };`}
      </Script>
      <Script
        id="knogents-widget-script"
        data-id="YOUR_CHATBOT_ID"
        src="https://knogents.com/embed.js"
        strategy="afterInteractive"
      />
    </>
  );
}
03 / INLINE & VITE REACT

Inline Client Component with Route Cleanup

For /support or /help pages

Mount the chat interface inside a dedicated page (such as app/support/page.tsx) or a Vite React app. The useEffect cleanup prevents duplicate widgets during client-side navigation. Leave the global floating script out of app/layout.tsx when using inline mode.

components/KnogentsInlineChat.tsxClient Component
"use client";

import React, { useEffect } from "react";

declare global {
  interface Window {
    widgetConfig?: Record<string, unknown>;
    ChatWidget?: { destroy?: () => void };
    KnogentsWidget?: { destroy?: () => void };
    __knogents_widget_initialized?: boolean;
    __rag_widget_initialized?: boolean;
  }
}

export function KnogentsInlineChat() {
  useEffect(() => {
    window.widgetConfig = {
      chatbotId: "YOUR_CHATBOT_ID",
      display: "inline",
    };

    if (window.KnogentsWidget && typeof window.KnogentsWidget.destroy === "function") {
      window.KnogentsWidget.destroy();
    } else if (window.ChatWidget && typeof window.ChatWidget.destroy === "function") {
      window.ChatWidget.destroy();
    }
    window.__knogents_widget_initialized = false;
    window.__rag_widget_initialized = false;

    const oldScript = document.getElementById("knogents-widget-script");
    if (oldScript && oldScript.parentNode) {
      oldScript.parentNode.removeChild(oldScript);
    }

    const script = document.createElement("script");
    script.id = "knogents-widget-script";
    script.async = true;
    script.setAttribute("data-id", "YOUR_CHATBOT_ID");
    script.type = "text/javascript";
    script.src = "https://knogents.com/embed.js";
    document.body.appendChild(script);

    return () => {
      const activeScript = document.getElementById("knogents-widget-script");
      if (activeScript && activeScript.parentNode) {
        activeScript.parentNode.removeChild(activeScript);
      }
      if (window.KnogentsWidget && typeof window.KnogentsWidget.destroy === "function") {
        window.KnogentsWidget.destroy();
      } else if (window.ChatWidget && typeof window.ChatWidget.destroy === "function") {
        window.ChatWidget.destroy();
      }
      window.__knogents_widget_initialized = false;
      window.__rag_widget_initialized = false;
    };
  }, []);

  return (
    <div
      id="rag-inline-bot"
      style={{ width: "100%", height: "600px", minHeight: "450px" }}
    />
  );
}

What Runs Behind Your Next.js Widget

Skip maintaining your own vector database, document parsers, and cron crawlers.

Hydration-Safe, Under 20 KB Script

Standalone vanilla JavaScript with zero third-party libraries. Loads asynchronously after interactive hydration so it never bloats your Next.js client bundle or triggers React mismatch warnings.

Structured Excel, CSV, PDF & Docs RAG

Ingest multi-sheet .xlsx, .xls, and .csv pricing or limit matrices alongside .pdf, .docx, .md, and .txt files. Column headers stay attached across chunks in PostgreSQL pgvector.

Automated Sitemap Sync & Factual Grounding

Point Knogents at your sitemap.xml (including sitemaps generated by Next.js sitemap.ts or next-sitemap). Pages re-index automatically (monthly on Free, weekly on Basic, daily on Standard) and every response links back to its source.

In-Chat Scheduling & Email Lead Alerts

Capture visitor details or book consultations directly inside the widget with natural language date/time detection and conflict checking. Instant email alerts send leads straight to your inbox.

Knogents vs. Building DIY with the Vercel AI SDK

Decide whether a managed RAG widget or raw React primitives fits your sprint.

AI Chatbot for Next.js & React
RequirementKnogentsDIY with Vercel AI SDK
Time to ProductionUnder 3 minutesDays to weeks (parsers, vectors, UI)
PDF, Word & Excel Table RAGManaged ingestion & pgvector searchBuild chunking and vector DB yourself
Scheduled Sitemap SyncBuilt-in monthly, weekly, or daily syncBuild and host cron crawlers yourself
Headless React Hooks (useChat)Managed widget UI (themes + custom CSS)Full primitive hooks for custom UIs
Lead Capture & SchedulingBuilt-in meeting capture + email alertsCustom tool calls + email setup
PricingFlat monthly ($0, $29, $59) + add-onsOpen source SDK + LLM API & DB hosting bills

Flat Monthly Plans

Start free in development and upgrade when your Next.js app scales.

Free ($0/mo)

100 messages/mo, 1 agent, 30K chars, monthly sync, 0 extra seats (Owner only)

Start free
Basic ($29/mo)

1,000 messages/mo, 2 agents, 150K chars, weekly sync, 2 extra seats, 5 themes

Details
Standard ($59/mo)

3,000 messages/mo, 6 agents, 500K chars, daily sync, 3 extra seats, custom CSS

Details

Optional add-ons: +1,500 messages ($10/mo), +1 agent ($8/mo), Remove Branding ($10/mo).

Scope & Boundaries

Clear engineering boundaries before you install.

  • Managed widget UI: Customize colors, themes, or custom CSS on Standard, rather than building from raw headless React hooks.
  • No human live-chat inbox: Knogents is an automated AI agent. Captured leads and bookings go to your dashboard and email inbox.
  • Websites only: No social DM or external messaging inbox automation.
  • No ticketing CRM or native HubSpot/Salesforce sync: View conversation logs, analytics, and team access inside your Knogents workspace.

Frequently Asked Questions

Technical answers for deploying Knogents in Next.js and React codebases.

How do I add Knogents to Next.js App Router (app/layout.tsx)?

Import Script from 'next/script' inside app/layout.tsx and render two <Script> tags inside <body> with strategy="afterInteractive": one to set window.widgetConfig with your chatbotId, and one to load https://knogents.com/embed.js. Because next/script runs after hydration, there are no SSR mismatch errors.

Do I need to add 'use client' to app/layout.tsx?

No. Next.js supports next/script inside Server Components, so your root layout stays a Server Component when using the floating widget. You only need a Client Component if you mount an inline container inside a specific route.

How do I handle SPA route transitions for an inline chat container?

Use the KnogentsInlineChat Client Component below on routes that render <div id="rag-inline-bot" />. Its useEffect cleanup removes the previous script tag and resets the widget instance when users navigate between pages.

Does Knogents work with Next.js Pages Router or Vite React?

Yes. In Pages Router, place the same next/script tags inside pages/_app.tsx. In Vite or standard React SPAs, inject the script inside a useEffect hook with cleanup on unmount.

How does Knogents compare to building a chatbot with the Vercel AI SDK?

The Vercel AI SDK gives you low-level hooks like useChat, but you still have to build and host your own vector database, PDF and Excel parsers, sitemap crawler, and widget UI. Knogents ships all of that as a managed platform with flat monthly pricing ($0, $29, $59/mo). Note that Knogents does not expose headless React hooks for custom UIs.

How does human follow-up work when a visitor wants to talk to our team?

Knogents is an automated AI agent and does not include a shared live-chat helpdesk console. When a visitor asks for a consultation or human help, the agent captures their name, email, and notes (or checks scheduling conflicts and books a meeting slot) and sends an instant email notification to your inbox.

Ship Your Next.js AI Assistant in Minutes

Connect your sitemap, upload your docs or Excel sheets, and drop in the next/script snippet.

Solutions Powered by this Integration

Explore autonomous AI customer service and sales workflows built for your platform.

Setup Guides & Tutorials

Step-by-step technical guides for setting up Knogents on this platform.

Explore Other Platform Integrations

Deploy Knogents across Shopify, WordPress, WooCommerce, Wix, Webflow, and custom stacks.

24/7 STOREFRONT ASSISTANT

Deploy Knogents on Your Website in 2 Minutes

Connect your website sitemap, upload your documentation or product catalog, and embed with a single snippet.

Free tier includes 100 messages/mo
No credit card required
Under 20 KB vanilla script