Chatbot for Next.js Website Projects: Add the Script in Your Root Layout, Handle Client-Side Navigation and Keep the Key in an Environment Variable (Timeline)
Chatbot for Next.js website builds comes down to three choices: where the script loads, how it hears about client-side navigation, and where the key lives. LiveChatBot has no Next.js package, so you use the standard embed script with the framework’s own Script component. This guide walks through it in order.
โก In short
- No Next.js package exists. Load the embed script once with the framework’s Script component in the root layout.
- Next.js navigates on the client, so tell the widget about route changes with pageView.
- Keep the website key in an environment variable, one per environment.
- The widget is browser-only. It never runs on the server.

The shape of it
What a chatbot for Next.js website code actually involves
Next.js renders pages on the server, then hands control to the browser, where later navigation happens without a full reload. A chat widget is browser JavaScript, so two things follow.
Load it once, in the browser
The root layout wraps every page. A script added there loads for the whole session and is not re-added when the visitor moves between routes.
Do not expect server-side help
The widget needs a browser. Nothing runs during server rendering, and there is no server SDK. The key is public by nature, since it is in the page for every visitor, so using an environment variable is about convenience across environments, not secrecy.
The page object window.LiveChatBot offers open, close, show, hide, setVisitor, sendMessage, pageView and on. That is the complete list.
Try it
Your setup timeline, step by step
Follow the steps in order. The snippets are generic: adjust file names to your project structure.
In your LiveChatBot dashboard, add one website for production and one for staging. The free plan allows two. Copy each website key.
Add NEXT_PUBLIC_LCB_KEY to your environment files, with the production key in production and the staging key elsewhere. The NEXT_PUBLIC prefix makes it available in the browser.
In your root layout, import Script from next/script and render <Script src="https://livechatbot.in/widget.js" data-key={process.env.NEXT_PUBLIC_LCB_KEY} strategy="afterInteractive" /> inside the body.
Add a small client component that calls window.LiveChatBot.pageView() when the pathname changes, and render it in the layout.
After a real login, call setVisitor with a name and an email or phone number, from a client component.
Open the live site in a private window, click through three routes and check the page names in your dashboard.
Script component
Using the Script component the right way
The Script component from Next.js controls when a third-party script loads. The strategy named afterInteractive loads it after the page becomes interactive, which keeps the chat out of your first load. That suits a chat widget well.
Where to render it
In the App Router, put it in the root layout file, inside the body. In the Pages Router, put it in the custom App file so it renders once for all pages.
Attributes
Extra attributes such as data-key are passed through to the script tag. Use the script address and key exactly as your dashboard shows them.
Do not add it per page
Putting it in several page components means it can load more than once. One place, high in the tree, is enough. Our chatbot script guide explains what the line does.
Navigation
Client-side navigation in a chatbot for Next.js website routes
Moving between routes with the Link component does not reload the page. The widget therefore sees only the first URL unless you tell it.
App Router
Write a small client component, marked with use client, that reads the pathname from the navigation hooks and runs an effect when it changes. Inside the effect, check that window.LiveChatBot exists, then call pageView. Render the component once in the root layout.
Pages Router
Listen for the router’s routeChangeComplete event in the custom App file and call pageView there.
Why it matters
Page-based Flows and visitor tracking depend on the current page. If you skip this, a proactive message set for the pricing page will not fire when someone arrives by in-app navigation. If the first page appears twice in tracking, skip the call on the first run.
Environments
One key per environment, and what hydration means for you
Keys
Use the staging website for preview deployments so test traffic never uses live visitors. Environment variables set per environment in your host make this a configuration change, not a code change.
Hydration
Because the script is added by the framework and the widget builds its own interface after load, it should not cause hydration mismatches. If you see warnings, they usually come from your own code reading window during render, not from the widget. Keep any call to window.LiveChatBot inside effects or event handlers.
Static export
If you export the site as static files, the same layout approach works, and the script still loads in the browser. Our HTML rollout guide covers checking each page.
Testing
Test a chatbot for Next.js website deploys before customers see it
Build, not just dev
Run a production build locally and start it, since script loading and caching behave differently from the dev server. Open the page, look for the bubble, and send a message to confirm it reaches your inbox and the Android app.
Check the script count
View the page source or the elements panel and search for the widget address. There should be one script.
Walk three routes
Click through three routes with in-app links, not by typing addresses. Then check the visitor’s page history in the dashboard.
Look for blocked requests
A content security policy set in your Next.js headers can block the script. If the bubble is missing, open the console. The blocked address is listed there, and you add it to your policy.
Knowledge
Teach the bot what your app knows
The AI Agent answers from the knowledge you write, not from your codebase, database or API. List pricing, plans, limits, supported browsers, refund rules and where to find things in the app, in plain words. See how to train your AI chatbot.
For account-specific questions, such as a customer’s invoice or usage, hand the chat to a person or turn it into a ticket. Do not let the bot guess.
Honest gaps
What it will not do in your Next.js project
- No Next.js package, server component or SDK. Only the script and window.LiveChatBot.
- No native sign-in link to your auth provider. You pass the user yourself with setVisitor.
- No pre-chat form, no badge removal, no WhatsApp inbox, no iOS team app.
- It cannot read your routes, database or state. Data leaves through ticket webhooks, the REST API or Zapier.
Ask our team for developer documentation, and see pricing for packs after the free 100 visitors per website each month.
Frequently asked questions
Is there a Next.js package for LiveChatBot?
No. Use the embed script with the Script component from Next.js, plus the window.LiveChatBot API.
Where do I put the script in the App Router?
In the root layout, inside the body, so it loads once for every route.
Do I need to handle route changes?
Yes, if you use page-based Flows or care about page tracking. Call pageView after each client-side navigation.
Can I use a chatbot for Next.js website previews and staging?
Yes. Use the second free website and its own key for staging and preview deployments.
Is the website key secret?
No. It is visible in the page source, like any embed key. Keep API keys for the REST API on the server only.
Keep reading
Related guides
Chatbot for React Website Projects: Load the Script Once, Survive Route Changes and Avoid Double Installs (Copy-Ready Snippets)
Chatbot for Laravel Website Owners: Add the Embed Script to Blade the Safe Way, With Config Caching, Logged-In Names and Test Routes (Free Copy-Ready Snippets)
Chatbot for Custom Website Builds: Where the Script Goes in Your Templates, Which Environment Gets Which Key, and What Your Security Rules Must Allow
Add chat to your Next.js app
One script, a free plan for two websites and no per-seat charges. Start on livechatbot.in.