FastComments.com

Add Live Commenting to Sanity Sites

This guide adds the FastComments live commenting widget to a site whose content is managed in Sanity. Sanity stores content and serves it through its APIs but does not render the public site, so the widget is added to the frontend application that renders Sanity documents. Each comment thread is identified by the document's _id, so the thread stays attached to the document when its slug or URL changes.

Sanity does not provide comments for site visitors. Sanity Studio includes a Comments feature, available on the Growth plan, which lets Studio users comment on fields and text while editing content. These comments are for content teams and are not displayed on the public site.

This tutorial requires a FastComments account. You can create an account here.

Step 1: Query the Document's _id Internal Link

Every Sanity document has an _id, and the ID cannot be modified after the document is created. Using the _id as the FastComments thread ID keeps comments attached to the document when its slug, title, or URL changes.

The frontend usually loads a document by its slug. The examples in this guide use a document type named post with a slug field. Replace them with the names of your own document type and field.

Create a client with @sanity/client, for example in sanity/client.ts. Replace the project ID and dataset with the values of your Sanity project:

sanity/client.ts
Copy Copy
1
2import { createClient } from '@sanity/client';
3
4export const client = createClient({
5 projectId: 'your-project-id',
6 dataset: 'production',
7 useCdn: true,
8 apiVersion: '2025-02-19'
9});
10

Query the document by slug and select its _id with GROQ:

Fetch a Post by Slug
Copy Copy
1
2const post = await client.fetch(
3 '*[_type == "post" && slug.current == $slug][0]{_id, title}',
4 { slug: 'hello-world' }
5);
6// post._id is the thread ID, for example "ecfef291-60f0-4609-bbfc-263d11a48c43"
7

Drafts and the Published _id

Sanity stores an unpublished draft as a separate document whose ID is the published ID with the drafts. prefix, for example drafts.ecfef291-60f0-4609-bbfc-263d11a48c43. The published document's _id has no prefix. The frontend must use the published _id so that the draft and the published page show the same thread.

  • With API version 2025-02-19 and later, queries use the published perspective by default and return only published documents, so _id has no prefix.
  • With the drafts perspective, used for previews, _id is the published ID and the draft ID is in _originalId.
  • With the raw perspective, which is the default for API versions before 2025-02-19, an authenticated query can return draft documents alongside published ones. Set perspective: 'published' in createClient, or remove the drafts. prefix from _id before passing it to FastComments.

Step 2: Add the Widget in Next.js Internal Link

This step uses the Next.js App Router and the FastComments Next.js library, fastcomments-nextjs, which supports Next.js 13 to 16 with React 18 or 19. Install the library and the Sanity client in the Next.js project:

Install Packages
Copy Copy
1
2npm install fastcomments-nextjs @sanity/client
3

Render the Widget on the Post Page

The page is a Server Component. It queries the post from Sanity by slug and passes the post's _id to the comment widget as urlId. Create app/posts/[slug]/page.tsx, using the client from Step 1:

app/posts/[slug]/page.tsx
Copy Copy
1
2import { notFound } from 'next/navigation';
3import { FastComments } from 'fastcomments-nextjs';
4import { client } from '../../../sanity/client';
5
6const POST_QUERY = '*[_type == "post" && slug.current == $slug][0]{_id, title}';
7
8export default async function PostPage({ params }: { params: Promise<{ slug: string }> }) {
9 const { slug } = await params;
10 const post = await client.fetch(POST_QUERY, { slug });
11 if (!post) {
12 notFound();
13 }
14
15 return (
16 <article>
17 <h1>{post.title}</h1>
18 <FastComments
19 key={post._id}
20 tenantId="demo"
21 urlId={post._id}
22 url={`https://example.com/posts/${slug}`}
23 />
24 </article>
25 );
26}
27

If you are logged in to FastComments, the snippet above contains your tenant ID. Otherwise, replace demo with your tenant ID.

The example imports the client from sanity/client.ts in the project root. Adjust the import path to the location of your client file. Replace https://example.com/posts/ with the public URL of your post pages.

  • urlId identifies the comment thread. It is set to the post's _id.
  • url is the link to the page that FastComments uses in notifications.
  • key makes React mount a new widget when the reader navigates from one post to another.
  • The FastComments component declares 'use client' itself, so it can be rendered from a Server Component without a wrapper.

In Next.js 14 and earlier, params is a plain object instead of a Promise, and await on it returns the same object.

EU Accounts

If your FastComments account is in the EU region, add region="eu" to the FastComments component.

Add the Widget Without a Framework Library Internal Link

Frontends that do not use React, such as Astro, Nuxt, or server-rendered templates, can load the widget with the FastComments script. The template that renders the post writes the document's _id into a data-document-id attribute, and the script passes it to the widget as urlId:

Sanity FastComments Code
Copy Copy
1
2<div id="fastcomments-widget" data-document-id="SANITY_DOCUMENT_ID"></div>
3<script src="https://cdn.fastcomments.com/js/embed-v2.min.js"></script>
4<script>
5 (function () {
6 var container = document.getElementById('fastcomments-widget');
7 window.FastCommentsUI(container, {
8 tenantId: 'demo',
9 urlId: container.getAttribute('data-document-id'),
10 url: window.location.origin + window.location.pathname
11 });
12 })();
13</script>
14

If you are logged in to FastComments, the snippet above contains your tenant ID. Otherwise, replace demo with your tenant ID.

Replace SANITY_DOCUMENT_ID with the template expression for the published _id of the document. For example, in an Astro component the attribute is written as data-document-id={post._id}, and in a Vue template as :data-document-id="post._id".

If your FastComments account is in the EU region, load the script from https://cdn-eu.fastcomments.com/js/embed-v2.min.js and add region: 'eu' to the object passed to window.FastCommentsUI.

FastComments also provides component libraries for other frameworks, including Astro, Vue, and Svelte. With these libraries, pass the _id as the urlId option of the comment widget.

Enable or Disable Comments per Document Internal Link

A boolean field on the document type lets editors turn comments off for individual documents. The frontend reads the field and renders the widget only when comments are enabled.

Add the Field to the Schema

Add a commentsEnabled field to the document type's schema in your Studio project:

schemaTypes/post.ts
Copy Copy
1
2import { defineField, defineType } from 'sanity';
3
4export const post = defineType({
5 name: 'post',
6 title: 'Post',
7 type: 'document',
8 fields: [
9 // existing fields such as title and slug
10 defineField({
11 name: 'commentsEnabled',
12 title: 'Comments enabled',
13 type: 'boolean',
14 initialValue: true
15 })
16 ]
17});
18

initialValue sets the field to true on documents created in Studio after the field is added. Existing documents do not contain the commentsEnabled key until an editor sets the field, and GROQ returns null for the missing key.

Check the Field in the Frontend

Add the field to the GROQ projection:

GROQ Query
Copy Copy
1
2const POST_QUERY = '*[_type == "post" && slug.current == $slug][0]{_id, title, commentsEnabled}';
3

In the Next.js page from Step 2, render the FastComments component only when the field is not false:

Conditional Rendering
Copy Copy
1
2{post.commentsEnabled !== false && (
3 <FastComments
4 key={post._id}
5 tenantId="demo"
6 urlId={post._id}
7 url={`https://example.com/posts/${slug}`}
8 />
9)}
10

If you are logged in to FastComments, the snippet above contains your tenant ID. Otherwise, replace demo with your tenant ID.

The check uses !== false so that documents without a value for the field still show comments. To hide comments on documents without a value instead, use post.commentsEnabled === true.

Hiding the widget does not delete the document's existing comments. They are shown again when the field is set back to true.

Customization Internal Link

Styling and configuration options for the widget are described in the Customization Documentation.

With the Next.js library, configuration options are passed as props of the FastComments component. With the script embed, they are added to the object passed to window.FastCommentsUI.