TrolyPage

An autonomous Marketing & Sales operating system for businesses that want AI-powered omni-channel growth.

Contact Info

Follow Us

Website Live Chat 2 min read

How to Add a Chat Widget to a Next.js App

TrolyPage

Nov 19, 2026
9 views
How to Add a Chat Widget to a Next.js App

To add a chat widget to a Next.js App Router app:

  1. Open app/layout.tsx and import Script from next/script.
  2. Render one <Script> with strategy="afterInteractive" in the root layout, passing the widget's src and data attributes.
  3. Deploy, then check a public route.

Render it only once, in the root layout, so it isn't duplicated across routes.

Example

Copy the public embed values from the Website Chat snippet generated in Studio. The browser can read these values. Keep workspace API keys and other private credentials on the server.

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

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script
          src="[SRC_FROM_STUDIO]"
          data-token="[TOKEN_FROM_STUDIO]"
          data-server="[SERVER_FROM_STUDIO]"
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}
Install on Next.js (App Router)

Steps

  1. Publish your Website Chat channel in Studio and copy the snippet.
  2. Transfer src, data-token and data-server exactly to the Script component.
  3. Deploy.
  4. Open a public route on the deployed domain, confirm the widget, then select Check again in Studio.

Guide: Install Website Chat on Next.js.

Gotchas

IssueFix
Widget loads twiceRemove extra Script tags from nested layouts or pages
Blocked by CSPAllow the widget's script and connection domains in your Content Security Policy
Doesn't load locallyMake sure the domain you're testing is allowed in Studio
Pages Router projectPut an afterInteractive script in pages/_app.tsx for site-wide loading or on a specific page. Keep a single instance.

Test Next.js live chat after publishing

Test a hard refresh, then navigate between two App Router pages with client-side links. Check that chat loads once, that navigation preserves the intended conversation, and that pages excluded from the widget never load its script.

FAQ

Why afterInteractive?

It loads the script after the page becomes interactive, so it doesn't block initial rendering.

Can I identify logged-in users?

Check TrolyPage's current Website Chat documentation for identity options before passing user data.

References

Written by the TrolyPage team. Updated November 2026.