FastComments.com

Add Live Commenting to Strapi Sites

This guide adds the FastComments live commenting widget to a site whose content is managed in Strapi. Strapi is a headless CMS and does not render pages, so the widget is added to the frontend application that renders Strapi entries. Each comment thread is identified by the entry's documentId, so the thread stays attached to the entry when its slug or URL changes.

Strapi does not include a comment system for site visitors. The Strapi Marketplace lists third-party plugins for this, such as Comments (strapi-plugin-comments, maintained by Sensinum), which stores comments as content types in the Strapi database. FastComments stores comments on FastComments servers and does not require a Strapi plugin.

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

Step 1: Fetch the Entry's documentId Internal Link

In Strapi 5, a document groups the draft and published versions of an entry for every locale, and the REST API identifies a document by its documentId. Using the documentId as the FastComments thread ID keeps comments attached to the entry when its slug, title, or URL changes.

The frontend usually loads an entry by its slug. The examples in this guide use a collection type with the plural API ID articles and a slug field. Replace them with the names of your own content type and field.

Find an Entry by Slug
Copy Copy
1
2GET /api/articles?filters[slug][$eq]=hello-world
3

The response contains the matching entries in the data array. Each entry includes its documentId:

Response
Copy Copy
1
2{
3 "data": [
4 {
5 "id": 12,
6 "documentId": "znrlzntu9ei5onjvwfaalu2v",
7 "title": "Hello World",
8 "slug": "hello-world"
9 }
10 ],
11 "meta": {
12 "pagination": { "page": 1, "pageSize": 25, "pageCount": 1, "total": 1 }
13 }
14}
15

Use data[0].documentId as the thread ID. Do not use the numeric id: in Strapi 5 it differs between locales of the same document.

Access to the REST API

Content types are private by default. To allow the frontend to read entries, do one of the following:

  • In the admin panel, open Users & Permissions plugin > Roles, edit the Public role, enable the find action for the content type, and click Save.
  • Create a Read-only token in Settings > Global settings > API Tokens and send it in the Authorization: Bearer <token> header. Send the token only from server-side code so that it is not exposed to browsers.

Published content and locales

The REST API returns the published version of documents unless the request sets status=draft. The documentId is the same for the draft and the published version.

All locales of a document share one documentId, so every locale of an entry shows the same comment thread. To keep a separate thread per locale, combine the two values, for example documentId + '-' + locale.

Strapi 4

In Strapi 4, the REST API response does not include documentId. Each entry has a numeric id and its fields are nested under attributes. Use data[0].id as the thread ID and read fields from data[0].attributes.

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 in the Next.js project:

Install the FastComments Next.js Library
Copy Copy
1
2npm install fastcomments-nextjs
3

Render the Widget on the Entry Page

The page is a Server Component. It fetches the entry from Strapi by slug and passes the entry's documentId to the comment widget as urlId. Create app/articles/[slug]/page.tsx:

app/articles/[slug]/page.tsx
Copy Copy
1
2import { notFound } from 'next/navigation';
3import { FastComments } from 'fastcomments-nextjs';
4
5async function getArticle(slug: string) {
6 const res = await fetch(
7 `${process.env.STRAPI_URL}/api/articles?filters[slug][$eq]=${encodeURIComponent(slug)}`,
8 { headers: { Authorization: `Bearer ${process.env.STRAPI_API_TOKEN}` } }
9 );
10 if (!res.ok) {
11 throw new Error(`Strapi request failed with status ${res.status}`);
12 }
13 const json = await res.json();
14 return json.data[0];
15}
16
17export default async function ArticlePage({ params }: { params: Promise<{ slug: string }> }) {
18 const { slug } = await params;
19 const article = await getArticle(slug);
20 if (!article) {
21 notFound();
22 }
23
24 return (
25 <article>
26 <h1>{article.title}</h1>
27 <FastComments
28 key={article.documentId}
29 tenantId="demo"
30 urlId={article.documentId}
31 url={`https://example.com/articles/${slug}`}
32 />
33 </article>
34 );
35}
36

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

Set STRAPI_URL and STRAPI_API_TOKEN in the environment of the Next.js app, for example in .env.local. If the Public role has the find permission, remove the headers option. Replace https://example.com/articles/ with the public URL of your article pages.

  • urlId identifies the comment thread. It is set to the entry's documentId.
  • 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 entry 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 entry writes the entry's documentId into a data-entry-id attribute, and the script passes it to the widget as urlId:

Strapi FastComments Code
Copy Copy
1
2<div id="fastcomments-widget" data-entry-id="ENTRY_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-entry-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 ENTRY_DOCUMENT_ID with the template expression for the entry's documentId. For example, in an Astro component the attribute is written as data-entry-id={article.documentId}, and in a Vue template as :data-entry-id="article.documentId".

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 documentId as the urlId option of the comment widget.

Enable or Disable Comments per Entry Internal Link

A Boolean field on the content type lets editors turn comments off for individual entries. The frontend reads the field and renders the widget only when comments are enabled.

Add the Field in the Content-Type Builder

The Content-Type Builder is available only when Strapi runs in development mode.

  1. Open the Content-Type Builder and select the collection type, for example Article.
  2. Click Add another field and choose Boolean.
  3. Enter commentsEnabled as the name. In the advanced settings, set the default value to true.
  4. Click Finish, then click Save.

The Content-Type Builder writes the field to the content type's schema.json file, for example src/api/article/content-types/article/schema.json. The attribute can also be added to that file directly:

schema.json attribute
Copy Copy
1
2"attributes": {
3 "commentsEnabled": {
4 "type": "boolean",
5 "default": true
6 }
7}
8

Check the Field in the Frontend

The REST API returns the field with the other fields of the entry. 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{article.commentsEnabled !== false && (
3 <FastComments
4 key={article.documentId}
5 tenantId="demo"
6 urlId={article.documentId}
7 url={`https://example.com/articles/${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 entries without a value for the field, such as entries created before the field was added, still show comments.

Hiding the widget does not delete the entry'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.