In Next.js, the chat goes in your root layout with the built-in Script component. It loads once and stays across page changes.
Get your tag
It's in the Dinghy operator app, under Settings > Install. You need two values from it, the script address and the data-key:
<script src="https://chat.example.com/widget/loader.js" data-key="wk_your_public_key" async></script>
Add your site under Allowed sites in Settings > Install, like https://www.example.com. For local work, add http://localhost:3000 too.
App Router
In app/layout.tsx:
import Script from "next/script";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script
src="https://chat.example.com/widget/loader.js"
data-key="wk_your_public_key"
strategy="lazyOnload"
/>
</body>
</html>
);
}
lazyOnload loads the script when the browser is idle. That's what the Next.js docs suggest for chat widgets. The data-key attribute goes straight through to the script tag.
Pages Router
Same component, in 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://chat.example.com/widget/loader.js"
data-key="wk_your_public_key"
strategy="lazyOnload"
/>
</>
);
}
Show who is chatting
If your app has accounts, your server can tell Dinghy who the user is. Then their chat follows them across devices, and your team sees their name and email. You can add attributes too, like their plan. The add to your site page shows how.
Your server signs a short token with the workspace's signing secret. It's in Settings > Install, under Identify signed-in users. Keep that secret on the server. Add this route handler with the jose package:
// app/api/dinghy-token/route.ts
import { SignJWT } from "jose";
import { getUser } from "@/lib/auth"; // your own auth
const secret = new TextEncoder().encode(process.env.DINGHY_SIGNING_SECRET);
export async function POST() {
const user = await getUser();
if (!user) return new Response("Not signed in", { status: 401 });
const token = await new SignJWT({ name: user.name, email: user.email })
.setProtectedHeader({ alg: "HS256" })
.setAudience("wk_your_public_key")
.setSubject(String(user.id))
.setIssuedAt()
.setExpirationTime("1h")
.sign(secret);
return new Response(token);
}
Then, in a client component that renders for signed-in users:
"use client";
import { useEffect } from "react";
declare global {
interface Window {
Dinghy?: { q?: unknown[][]; identify?: (token: () => Promise<string>) => void };
}
}
export function DinghyIdentify() {
useEffect(() => {
const getToken = async () => {
const response = await fetch("/api/dinghy-token", { method: "POST" });
if (!response.ok) throw new Error("Could not get a Dinghy token");
return response.text();
};
if (window.Dinghy?.identify) window.Dinghy.identify(getToken);
else (window.Dinghy ??= { q: [] }).q?.push(["identify", getToken]);
}, []);
return null;
}
If the component runs before the chat script loads, the call waits in the queue. When the user signs out, call Dinghy.reset(), so the next person on that computer can't read the chat. The full token rules are in Show who is chatting.
If you use a Content Security Policy
Allow your Dinghy address in script-src, connect-src and img-src. Dinghy doesn't need unsafe-inline.