FastComments.com

Add Live Commenting to Sites Built with Storyblok

This guide adds the FastComments live commenting widget to a website or app whose content is stored in Storyblok.

Storyblok is a headless CMS and does not render the public site. The widget is added to the frontend that fetches stories from the Content Delivery API and renders them, and each comment thread is tied to the story's UUID rather than to its slug or URL. Storyblok's own comments, opened from the comment button on each field in the Visual Editor, are discussions between members of the space about a story's content. They are not shown to visitors of the site.

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

Get the Story UUID Internal Link

The Content Delivery API returns two identifiers for each story:

  • uuid: the unique identifier of the story in the space, for example a3b5c7d9-1234-4f56-8a90-bcdef1234567.
  • id: the legacy numeric story ID. Storyblok keeps it in API responses for existing integrations and recommends uuid for new projects.

Use the story's uuid as the FastComments urlId. A story's full_slug includes the path of its parent folder, so it changes when an editor edits the slug or moves the story to another folder. The uuid does not depend on the slug or the folder, so the comment thread stays attached to the story when its URL changes.

Storyblok's current Next.js setup uses the @storyblok/api-client package for the Content Delivery API and @storyblok/react to render blocks:

Install the Storyblok Packages
Copy Copy
1
2npm install @storyblok/react @storyblok/api-client
3

The API client is created in src/lib/storyblok.js, as in Storyblok's Next.js guide:

src/lib/storyblok.js
Copy Copy
1
2import { createApiClient } from '@storyblok/api-client';
3import { defineStoryblokBlocks } from '@storyblok/react';
4
5import Page from '@/components/Page';
6
7export const apiClient = createApiClient({
8 accessToken: process.env.STORYBLOK_DELIVERY_API_TOKEN,
9 region: 'eu', // The region of your Storyblok space: eu, us, ap, ca, or cn.
10});
11
12export const { StoryblokBlock } = defineStoryblokBlocks(() => ({
13 components: {
14 page: Page,
15 },
16}));
17

apiClient.stories.get takes a story's full slug, numeric ID, or UUID, and returns an object with data and error properties. The story is in data.story, and its UUID is data.story.uuid:

Read the Story UUID
Copy Copy
1
2const { data, error } = await apiClient.stories.get('blog/my-first-post', {
3 query: { version: 'published' },
4});
5if (error) {
6 throw error;
7}
8
9const storyUuid = data.story.uuid;
10

version: 'published' returns the published version of the story. Requesting version: 'draft' requires a preview access token.

Projects that use the older storyblok-js-client package get the same story object from storyblok.get('cdn/stories/blog/my-first-post', { version: 'published' }), in data.story.

Add the Widget in Next.js Internal Link

This example 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 FastComments Next.js Library
Copy Copy
1
2npm install fastcomments-nextjs
3

The page below is a server component for the route /blog/[slug]. It assumes the blog posts are stories in a folder with the slug blog, so the full slug of a post is blog/<slug>. It renders the story content with StoryblokBlock and then the comment widget, with the story UUID as the urlId:

src/app/blog/[slug]/page.js
Copy Copy
1
2import { notFound } from 'next/navigation';
3import { FastComments } from 'fastcomments-nextjs';
4import { apiClient, StoryblokBlock } from '@/lib/storyblok';
5
6export default async function BlogPostPage({ params }) {
7 const { slug } = await params;
8 const { data, error } = await apiClient.stories.get(`blog/${slug}`, {
9 query: { version: 'published' },
10 });
11
12 if (error?.response.status === 404) {
13 notFound();
14 }
15 if (error) {
16 throw error;
17 }
18
19 const story = data.story;
20
21 return (
22 <>
23 <StoryblokBlock block={story.content} />
24 <FastComments
25 key={story.uuid}
26 tenantId="demo"
27 urlId={story.uuid}
28 />
29 </>
30 );
31}
32

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

Notes on the example:

  • The FastComments component declares 'use client' itself, so it can be rendered from a server component without a wrapper.
  • The Storyblok API client runs only on the server in this page, so the access token is not sent to the browser and STORYBLOK_DELIVERY_API_TOKEN does not need the NEXT_PUBLIC_ prefix.
  • The key prop makes React mount a new widget when the reader navigates from one story to another.
  • params is a promise in Next.js 15 and later. In Next.js 13 and 14 it is a plain object, and await on it returns the same object.
  • For accounts in the EU region, add region="eu" to the FastComments component. This setting is independent of the region of the Storyblok space.

The comment widget uses the current page URL as the link for notification emails and the moderation page. To link comments to a different URL, pass a url prop. See How Comments are Tied to Pages and Articles.

For React applications that do not use Next.js, the React library provides the FastCommentsCommentWidget component, which takes the same tenantId and urlId props.

To let editors choose which stories have comments and where the widget appears in the story, render the widget from a block instead, as described in Add a FastComments Block for Editors.

Add a FastComments Block for Editors Internal Link

Instead of rendering the widget on every story of a route, the frontend can render it from a block. Editors add the block to the stories that should have comments, at the position where the comments should appear. This replaces the FastComments component in the page from Add the Widget in Next.js.

Create the Block in Storyblok

  1. In your Storyblok space, open the Block Library and select + New Block.
  2. Choose the Nestable block type.
  3. Enter fastcomments as the technical name. The block needs no fields.

Editors can then add the block to a story's blocks field, such as the body field of the page content type, in the Visual Editor. In the API response, the block appears in the field with component: 'fastcomments'.

Render the Block in the Frontend

A block component receives the block data, which does not include the story UUID. StoryblokBlock passes any props other than block to the registered component, so the page passes the story UUID down as a storyUuid prop.

Create the component for the block:

src/components/FastCommentsBlock.jsx
Copy Copy
1
2import { FastComments } from 'fastcomments-nextjs';
3
4export default function FastCommentsBlock({ storyUuid }) {
5 if (!storyUuid) {
6 return null;
7 }
8
9 return (
10 <FastComments
11 key={storyUuid}
12 tenantId="demo"
13 urlId={storyUuid}
14 />
15 );
16}
17

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

Register it in src/lib/storyblok.js. The key must match the block's technical name:

src/lib/storyblok.js
Copy Copy
1
2import { createApiClient } from '@storyblok/api-client';
3import { defineStoryblokBlocks } from '@storyblok/react';
4
5import Page from '@/components/Page';
6import FastCommentsBlock from '@/components/FastCommentsBlock';
7
8export const apiClient = createApiClient({
9 accessToken: process.env.STORYBLOK_DELIVERY_API_TOKEN,
10 region: 'eu', // The region of your Storyblok space: eu, us, ap, ca, or cn.
11});
12
13export const { StoryblokBlock } = defineStoryblokBlocks(() => ({
14 components: {
15 page: Page,
16 fastcomments: FastCommentsBlock,
17 },
18}));
19

Pass storyUuid from the page component to the blocks in its body field:

src/components/Page.jsx
Copy Copy
1
2import { StoryblokBlock } from '@/lib/storyblok';
3
4export default function Page({ block, storyUuid }) {
5 return (
6 <main>
7 {block.body?.map((nestedBlock) => (
8 <StoryblokBlock
9 block={nestedBlock}
10 key={nestedBlock._uid}
11 storyUuid={storyUuid}
12 />
13 ))}
14 </main>
15 );
16}
17

In the route, pass the story UUID to the top-level StoryblokBlock, and remove the FastComments component and its import:

src/app/blog/[slug]/page.js
Copy Copy
1
2import { notFound } from 'next/navigation';
3import { apiClient, StoryblokBlock } from '@/lib/storyblok';
4
5export default async function BlogPostPage({ params }) {
6 const { slug } = await params;
7 const { data, error } = await apiClient.stories.get(`blog/${slug}`, {
8 query: { version: 'published' },
9 });
10
11 if (error?.response.status === 404) {
12 notFound();
13 }
14 if (error) {
15 throw error;
16 }
17
18 return <StoryblokBlock block={data.story.content} storyUuid={data.story.uuid} />;
19}
20

The page component in Storyblok's Next.js guide already renders a <main> element, so the route returns StoryblokBlock without a wrapper.

If the FastComments block is placed inside another block, such as a grid, that block's component must also pass storyUuid to its nested StoryblokBlock elements. Otherwise the FastComments block receives no UUID and renders nothing.

Add the Widget Without a Framework Library Internal Link

Frontends that render HTML from a template, or that do not use a FastComments framework library, can load the widget with the FastComments VanillaJS script. The template writes the story's uuid into a data-story-uuid attribute on the widget container, and the script passes it to the widget as the urlId.

Add the following to the template that renders a story, at the position where the comments should appear. Replace STORY_UUID with the template expression that outputs the story's uuid.

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

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

For accounts in the EU region, load the script from https://cdn-eu.fastcomments.com/js/embed-v2.min.js and add region: 'eu' to the configuration object.

In a single-page application that fetches stories in the browser, pass story.uuid to urlId directly instead of reading it from an attribute. @storyblok/api-client supports current versions of Chrome, Edge, Firefox, and Safari.

FastComments also has libraries for other frontend frameworks. Each of them accepts a urlId option that takes the story UUID in the same way:

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 VanillaJS script, they are added to the object passed to window.FastCommentsUI. Many options can also be changed without code on the widget customization page of your FastComments account.

Options can also come from the FastComments block. For example, a Boolean field added to the fastcomments block in the Block Library is available on the block prop of FastCommentsBlock and can be passed to the widget, such as readonly={block.readonly} for a field with the technical name readonly. See Prevent New Replies & Disable Votes.

To show signed-in users of your site as the comment authors, use FastComments Secure SSO. The SSO verification hash is an HMAC-SHA256 hash computed with your FastComments API secret, so it must be generated in server-side code, such as a Next.js server component, and passed to the widget. Do not compute it in browser JavaScript or include the API secret in client-side code.