# Add emoji reactions to a Next.js site

> Add live emoji reactions to a Next.js App Router blog: the loader via next/script in app/layout.tsx, a floating bar in app/blog/[slug]/page.tsx, an inline bar per post keyed to the slug. TSX snippets included.

A Next.js blog gets live emoji reactions without an API route or a database: the loader goes in `app/layout.tsx` through `next/script`, a floating bar goes in `app/blog/[slug]/page.tsx`, and each post on the blog index gets an inline bar keyed to its slug. The snippets are for the App Router, with notes for the Pages Router and Next.js 14.

## Before you start

You need a Tapbar: the reactions, the look, and a publishable key (`pk_live_…`). [Sign up](https://app.tapback.party), create a Tapbar with your site as an allowed site (add `http://localhost:*` too, to try it on a dev server), and open its **Embed** panel — the loader tag there already carries your key. Your AI agent can do this part for you; see [the last section](https://docs.tapback.party/install/nextjs/#let-your-agent-do-it).

## Add the loader once

The loader goes in the root `app/layout.tsx`, via `next/script` with `type="module"`. It defines the `<tapback-bar>` element and starts every bar on the page, however many there are:

```tsx
import Script from 'next/script';
// inside <body>:
// add inside the starter's RootLayout <body>, after {children}:
<Script type="module" src="https://api.tapback.party/loader.js" strategy="afterInteractive"
        data-key="pk_live_…" data-api="https://api.tapback.party" />
```

Swap `pk_live_…` for your own key, or paste the tag from the Embed panel as it is.

## Put a floating bar on each post

In `app/blog/[slug]/page.tsx`, add one bar with `layout="float"`, keyed to the same `/blog/${slug}`. It docks in the bottom corner wherever it sits in the markup, and it connects live, so readers see each other's reactions fall.

```tsx
// app/blog/[slug]/page.tsx — params is a Promise on Next.js 15+, so await it.
// With cacheComponents on (create-next-app sets it), `next build` needs generateStaticParams here.
export async function generateStaticParams() {
  return (await getPosts()).map((post) => ({ slug: post.slug })); // your data layer
}
export default async function Post({ params }: { params: Promise<{ slug: string }> }) {
  const { slug } = await params;
  return (
    <article>
      {/* …your post… */}
      {/* @ts-expect-error — custom element */}
      <tapback-bar entity={`/blog/${slug}`} layout="float" />
    </article>
  );
}
```

## Add an inline bar to each post in the list

In `app/blog/page.tsx`, give every post its own bar, keyed to `/blog/${post.slug}`:

```tsx
// app/blog/page.tsx
{posts.map((post) => (
  <article key={post.slug}>
    <h2><a href={`/blog/${post.slug}`}>{post.title}</a></h2>
    <p>{post.excerpt}</p>
    {/* @ts-expect-error — custom element */}
    <tapback-bar entity={`/blog/${post.slug}`} layout="inline" live="off" />
  </article>
))}
```

`entity` names the counter. On a list page it has to be set by hand: a bar without it infers the list page's own URL, so every blurb would share one tally. Emit the same value the post page uses and a reaction on the blurb lands on the same count as one on the post.

`live="off"` shows each blurb's count without opening a live socket for it; the falling reactions stay on the post page. Bars that load together share one session request (up to 50 bars each), so a reader can reload a 50-post index about ten times a minute; past a few hundred bars on one page, paginate — see [Troubleshooting](https://docs.tapback.party/troubleshooting/#a-long-list-page-loses-some-bars).

## Different reactions per post (Plus)

By default every post shows your Tapbar's reactions. On [Plus](https://docs.tapback.party/plans/), a post can carry its own: add a named set in the console's Reactions card (say `spooky`, with a ghost and a skull), then point the post's bar at it.

```tsx
// app/blog/[slug]/page.tsx — post is your data layer's record for this slug
{/* @ts-expect-error — custom element */}
<tapback-bar entity={`/blog/${slug}`} layout="float" set={post.tapbackSet} />
```

Add a `tapbackSet` field to the posts that need one (front matter, CMS field, wherever your posts come from). React leaves the attribute off when the value is undefined, so other posts get the default bar. To list reactions on the bar instead of naming a set, use `reactions="ghost,skull"`; the keys have to be on the Tapbar or in its palette.

The list-page bars need no `set`. A `live="off"` bar that declares nothing never decides a post's reactions, so the post page's attribute decides them however readers arrive. The attribute applies the first time anyone loads the post; to change a post that's already live, use the ☰ picker in the console's **Pages** tab or the `set_entity_reactions` MCP tool. [Changing reactions on live pages](https://docs.tapback.party/live-pages/) has the full rules.

To choose a post's reactions by looking at the draft instead, add the [in-page picker](https://docs.tapback.party/authoring/) to the draft render: gate the editor `<script>` on `(await draftMode()).isEnabled` from `next/headers` (or `process.env.NODE_ENV === 'development'` without draft mode) and render `<tapback-editor>` in that same branch of `app/blog/[slug]/page.tsx`. Read draft mode in the page, not the root layout: in the layout it makes every route dynamic.

## Check it works

1. Deploy to a site on the Tapbar's allowed list (or run your dev server, with `http://localhost:*` allowed). The bar only runs on allowed sites.
2. Open a post, tap a reaction, and reload. The count is still there.
3. Open the post list. That post's blurb shows the same count.

If no bar appears, check that the loader tag is in that page's `<head>` and that the page's origin is allowed — `www` and the bare domain are separate entries. [Troubleshooting](https://docs.tapback.party/troubleshooting/) maps each symptom to its fix.

## Notes for Next.js

- `<tapback-bar>` is a custom element: TypeScript needs `@ts-expect-error` on each use or a JSX declaration for the tag, which [the Next.js recipe](https://docs.tapback.party/blog-reactions/#next-js) has.
- The `type="module"` matters: `loader.js` is an ES module, and a classic `<script>` throws on its `import`. `next/script` with `afterInteractive` injects the tag after hydration, so it isn't in the page source (only a preload link is), and that's expected.
- If you load the loader with a plain `<script type="module">` in the layout or with `beforeInteractive` instead, it runs before React hydrates and dev mode logs a hydration mismatch on each `layout="float"` bar (the loader sets `display: contents` on it). Add `suppressHydrationWarning` to those bars. With `afterInteractive` there's nothing to suppress.
- There's no framework permalink — build the post path once in a helper so the index and the `[slug]` route can't diverge.
- On Next.js 14 and earlier `params` is a plain object, so `params.slug` works without the `await`. The Pages Router is the same idea: the loader goes in `pages/_app.tsx`, the routes in `pages/blog/index.tsx` and `pages/blog/[slug].tsx`.
- With `cacheComponents` and `partialPrefetching` on (both in create-next-app's config), `next dev` logs "encountered URL data during prerendering" at `await params` in this page. It's a dev-only message and the build is fine; `export const instant = false;` in the page, or rendering the body inside `<Suspense>`, silences it.
- `next/script` adds a classic-script preload for the loader, so DevTools may show two "preload not used" warnings on each page. The bar is unaffected.
- With `cacheComponents` on, a client-side navigation keeps the previous page mounted but hidden. Its floating bar steps out of the corner and the new page's bar takes it; going back brings the earlier bar back with fresh counts.

## Let your agent do it

Connect your agent to the Tapback MCP server — in Claude Code, `claude mcp add --transport http tapback https://api.tapback.party/mcp`; in Claude Desktop or claude.ai, **Add custom connector** with `https://api.tapback.party/mcp` — then paste this prompt:

```
Add Tapback live reactions to my Next.js site.

1. Connect to the Tapback MCP server at https://api.tapback.party/mcp and let me approve the sign-in.
2. Ask me for my site's domain, then create a Tapbar with that domain as its allowed site. Give it a heart, a star, and a smile to start — I can change these later.
3. Show me the embed snippet and tell me exactly where it goes in Next.js: the loader `<script>` in the root `app/layout.tsx`, via `next/script` with `type="module"`; an inline bar (`layout="inline"`, `live="off"`) in `app/blog/page.tsx`, keyed to `/blog/${post.slug}`; and a floating bar (`layout="float"`) in `app/blog/[slug]/page.tsx`, keyed to the same `/blog/${slug}`. `<tapback-bar>` is a custom element: TypeScript needs `@ts-expect-error` on each use or a JSX declaration for the tag, which [the Next.js recipe](https://docs.tapback.party/blog-reactions/#next-js) has. The Next.js guide is at https://docs.tapback.party/install/nextjs/ (as markdown: https://docs.tapback.party/install/nextjs/index.md).
4. Once I've deployed, post a test reaction and read the count back so we know it's working.
```

[Set up with your agent](https://docs.tapback.party/agent-setup/) has the same prompt for every stack, with an optional step for the in-page picker. [Manage from your agent](https://docs.tapback.party/mcp/) lists everything the agent can reach.
