FastComments.com

为使用 Storyblok 构建的网站添加实时评论功能

本指南介绍如何将 FastComments 实时评论组件添加到内容存储在 Storyblok 中的网站或应用。

Storyblok 是一个无头 CMS,不负责渲染公开站点。该组件被添加到从 Content Delivery API 获取故事并进行渲染的前端中,每个评论线程都与故事的 UUID 绑定,而不是与其 slug 或 URL 绑定。Storyblok 自带的评论功能(从 Visual Editor 中每个字段上的评论按钮打开)是空间成员之间关于故事内容的讨论,不会向站点访客展示。

本教程需要一个 FastComments 账户。你可以在此创建账户。

获取 Story UUID Internal Link

Content Delivery API 为每个 story 返回两个标识符:

  • uuid:story 在空间中的唯一标识符,例如 a3b5c7d9-1234-4f56-8a90-bcdef1234567。
  • id:旧版的数字 story ID。Storyblok 会在 API 响应中保留它以支持现有集成,并建议新项目使用 uuid。

请使用 story 的 uuid 作为 FastComments 的 urlId。story 的 full_slug 包含其父文件夹的路径,因此当编辑者修改 slug 或将 story 移动到其他文件夹时,它会发生变化。而 uuid 不依赖于 slug 或文件夹,因此即使 story 的 URL 发生变化,评论线程仍会保持与该 story 的关联。

Storyblok 当前的 Next.js 设置使用 @storyblok/api-client 包来访问 Content Delivery API,并使用 @storyblok/react 来渲染区块:

安装 Storyblok 包
Copy Copy
1
2npm install @storyblok/react @storyblok/api-client
3

API 客户端在 src/lib/storyblok.js 中创建,与 Storyblok 的 Next.js 指南一致:

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', // 你的 Storyblok 空间所在区域:eu、us、ap、ca 或 cn。
10});
11
12export const { StoryblokBlock } = defineStoryblokBlocks(() => ({
13 components: {
14 page: Page,
15 },
16}));
17

apiClient.stories.get 接受 story 的完整 slug、数字 ID 或 UUID,并返回一个包含 data 和 error 属性的对象。story 位于 data.story 中,其 UUID 为 data.story.uuid:

读取 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' 返回 story 的已发布版本。请求 version: 'draft' 则需要预览访问令牌。

使用较旧的 storyblok-js-client 包的项目可以通过 storyblok.get('cdn/stories/blog/my-first-post', { version: 'published' }) 获取相同的 story 对象,位于 data.story 中。

在 Next.js 中添加组件 Internal Link

此示例使用 Next.js App Router 和 FastComments Next.js 库 fastcomments-nextjs,它支持 Next.js 13 到 16 以及 React 18 或 19。

安装 FastComments Next.js 库
Copy Copy
1
2npm install fastcomments-nextjs
3

下面的页面是路由 /blog/[slug] 的服务器组件。它假设博客文章是 slug 为 blog 的文件夹中的故事(story),因此文章的完整 slug 为 blog/<slug>。它使用 StoryblokBlock 渲染故事内容,然后渲染评论组件,并将故事的 UUID 作为 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

如果您已登录 FastComments,上面的代码片段中已包含您的租户 ID(tenant ID)。否则,请将 demo 替换为您的租户 ID。

关于此示例的说明:

  • FastComments 组件自身声明了 'use client',因此可以直接从服务器组件渲染,无需包装器。
  • 在此页面中,Storyblok API 客户端仅在服务器端运行,因此访问令牌不会发送到浏览器,STORYBLOK_DELIVERY_API_TOKEN 不需要 NEXT_PUBLIC_ 前缀。
  • key 属性使 React 在读者从一个故事导航到另一个故事时挂载一个新组件。
  • 在 Next.js 15 及更高版本中,params 是一个 Promise。在 Next.js 13 和 14 中,它是一个普通对象,对其使用 await 会返回同一个对象。
  • 对于欧盟地区的账户,请在 FastComments 组件上添加 region="eu"。此设置与 Storyblok 空间(space)所在的地区无关。

评论组件使用当前页面的 URL 作为通知邮件和审核页面的链接。要将评论关联到不同的 URL,请传入 url 属性。参见评论如何与页面和文章关联。

对于不使用 Next.js 的 React 应用程序,React 库提供了 FastCommentsCommentWidget 组件,它接受相同的 tenantId 和 urlId 属性。

要让编辑选择哪些故事有评论以及组件在故事中出现的位置,可以改为从块(block)中渲染组件,如为编辑添加 FastComments 块中所述。

为编辑者添加 FastComments 区块 Internal Link

前端无需在路由的每个 story 中渲染组件,而是可以通过 block 来渲染。编辑者将 block 添加到需要评论的 story 中,并放置在评论应出现的位置。这取代了 Add the Widget in Next.js 中页面里的 FastComments 组件。

在 Storyblok 中创建 Block

  1. 在您的 Storyblok 空间中,打开 Block Library 并选择 + New Block。
  2. 选择 Nestable block 类型。
  3. 输入 fastcomments 作为技术名称。该 block 不需要任何字段。

然后,编辑者可以在 Visual Editor 中将该 block 添加到 story 的 blocks 字段,例如 page 内容类型的 body 字段。在 API 响应中,该 block 会以 component: 'fastcomments' 的形式出现在字段中。

在前端渲染 Block

Block 组件接收 block 数据,其中不包含 story 的 UUID。StoryblokBlock 会将除 block 以外的所有 props 传递给注册的组件,因此页面需要将 story 的 UUID 作为 storyUuid prop 向下传递。

创建该 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

如果您已登录 FastComments,上面的代码片段中已包含您的租户 ID。否则,请将 demo 替换为您的租户 ID。

在 src/lib/storyblok.js 中注册它。键名必须与 block 的技术名称一致:

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', // 您的 Storyblok 空间所在区域:eu、us、ap、ca 或 cn。
11});
12
13export const { StoryblokBlock } = defineStoryblokBlocks(() => ({
14 components: {
15 page: Page,
16 fastcomments: FastCommentsBlock,
17 },
18}));
19

在 page 组件中,将 storyUuid 传递给其 body 字段中的各个 block:

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

在路由中,将 story 的 UUID 传递给顶层的 StoryblokBlock,并移除 FastComments 组件及其导入:

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

Storyblok 的 Next.js 指南中的 page 组件已经渲染了 <main> 元素,因此路由可以直接返回 StoryblokBlock 而无需包装器。

如果 FastComments block 被放置在另一个 block 内部(例如 grid),该 block 的组件也必须将 storyUuid 传递给其嵌套的 StoryblokBlock 元素。否则,FastComments block 将收不到 UUID,从而不会渲染任何内容。

在不使用框架库的情况下添加组件 Internal Link

从模板渲染 HTML 的前端,或未使用 FastComments 框架库的前端,可以通过 FastComments VanillaJS 脚本加载评论组件。模板将文章的 uuid 写入评论组件容器上的 data-story-uuid 属性,脚本将其作为 urlId 传递给评论组件。

在渲染文章的模板中,将以下内容添加到评论应出现的位置。将 STORY_UUID 替换为输出文章 uuid 的模板表达式。

Storyblok FastComments 代码
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

如果您已登录 FastComments,上面的代码片段中已包含您的租户 ID。否则,请将 demo 替换为您的租户 ID。

对于欧盟地区的账户,请从 https://cdn-eu.fastcomments.com/js/embed-v2.min.js 加载脚本,并在配置对象中添加 region: 'eu'。

在浏览器中获取文章的单页应用中,直接将 story.uuid 传递给 urlId,而不是从属性中读取。@storyblok/api-client 支持当前版本的 Chrome、Edge、Firefox 和 Safari。

FastComments 还为其他前端框架提供了库。每个库都接受一个 urlId 选项,以同样的方式接收文章的 UUID:

自定义设置 Internal Link

小部件的样式和配置选项在自定义文档中有描述。

使用 Next.js 库时,配置选项作为 FastComments 组件的 props 传递。使用 VanillaJS 脚本时,它们被添加到传递给 window.FastCommentsUI 的对象中。许多选项也可以在您的 FastComments 账户的小部件自定义页面上无需代码进行更改。

选项也可以来自 FastComments 区块。例如,在区块库中添加到 fastcomments 区块的布尔字段可在 FastCommentsBlock 的 block prop 上使用,并可以传递给小部件,例如对于技术名称为 readonly 的字段,可以使用 readonly={block.readonly}。参见禁止新回复并禁用投票。

要将您网站的已登录用户显示为评论作者,请使用 FastComments Secure SSO。SSO 验证哈希是使用您的 FastComments API 密钥计算的 HMAC-SHA256 哈希,因此必须在服务器端代码(例如 Next.js 服务器组件)中生成并传递给小部件。请勿在浏览器 JavaScript 中计算它,也不要将 API 密钥包含在客户端代码中。