FastComments.com

為使用 Storyblok 建置的網站新增即時留言功能

本指南將 FastComments 即時留言小工具加入到內容儲存於 Storyblok 的網站或應用程式中。

Storyblok 是一個無頭(headless)CMS,並不負責渲染公開網站。小工具是加入到從 Content Delivery API 取得故事並加以渲染的前端,且每個留言串綁定的是故事的 UUID,而非其 slug 或 URL。Storyblok 自身的留言功能(從 Visual Editor 中每個欄位的留言按鈕開啟)是空間成員之間關於故事內容的討論,不會顯示給網站訪客。

本教學需要一個 FastComments 帳號。你可以在此建立帳號。

取得 Story UUID Internal Link

內容交付 API 為每個故事返回兩個識別碼:

  • uuid:故事在空間中的唯一識別碼,例如 a3b5c7d9-1234-4f56-8a90-bcdef1234567。
  • id:舊版的數字故事 ID。Storyblok 為了現有的整合在 API 回應中保留了它,並建議新專案使用 uuid。

請使用故事的 uuid 作為 FastComments 的 urlId。故事的 full_slug 包含其上層資料夾的路徑,因此當編輯者修改 slug 或將故事移動到另一個資料夾時,它會改變。uuid 不依賴於 slug 或資料夾,因此當故事的 URL 改變時,留言串仍會保持附加在該故事上。

Storyblok 目前的 Next.js 設定使用 @storyblok/api-client 套件來存取內容交付 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 接受故事的完整 slug、數字 ID 或 UUID,並返回一個包含 data 和 error 屬性的物件。故事位於 data.story,其 UUID 為 data.story.uuid:

讀取故事 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' 返回故事的已發布版本。請求 version: 'draft' 則需要預覽存取權杖。

使用較舊的 storyblok-js-client 套件的專案,可以透過 storyblok.get('cdn/stories/blog/my-first-post', { version: 'published' }) 取得相同的物件,位於 data.story 中。

在 Next.js 中新增小工具 Internal Link

此範例使用 Next.js App Router 以及 FastComments Next.js 函式庫 fastcomments-nextjs,該函式庫支援搭配 React 18 或 19 的 Next.js 13 至 16。

安裝 FastComments Next.js 函式庫
Copy Copy
1
2npm install fastcomments-nextjs
3

下方的頁面是路由 /blog/[slug] 的伺服器元件。它假設部落格文章是位於 slug 為 blog 的資料夾中的故事,因此文章的完整 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。否則,請將 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 會回傳相同的物件。
  • 對於 EU 區域的帳戶,請在 FastComments 元件中加入 region="eu"。此設定與 Storyblok 空間的區域無關。

留言小工具使用目前的頁面 URL 作為通知電子郵件與審核頁面的連結。若要將留言連結到不同的 URL,請傳入 url 屬性。請參閱留言如何與頁面和文章綁定。

對於不使用 Next.js 的 React 應用程式,React 函式庫提供了 FastCommentsCommentWidget 元件,它接受相同的 tenantId 和 urlId 屬性。

若要讓編輯者選擇哪些故事有留言以及小工具在故事中出現的位置,請改為從區塊渲染小工具,如為編輯者新增 FastComments 區塊中所述。

為編輯者新增 FastComments 區塊 Internal Link

前端可以改為從區塊渲染小工具,而不是在路由的每個 story 上渲染。編輯者將該區塊新增到需要留言的 story 中,並放在留言應出現的位置。這取代了 Add the Widget in Next.js 頁面中的 FastComments 元件。

在 Storyblok 中建立區塊

  1. 在您的 Storyblok 空間中,開啟 Block Library 並選擇 + New Block。
  2. 選擇 Nestable 區塊類型。
  3. 輸入 fastcomments 作為技術名稱。此區塊不需要任何欄位。

編輯者接著可以在 Visual Editor 中將該區塊新增到 story 的 blocks 欄位,例如 page 內容類型的 body 欄位。在 API 回應中,該區塊會以 component: 'fastcomments' 出現在欄位中。

在前端渲染區塊

區塊元件會接收區塊資料,其中不包含 story UUID。StoryblokBlock 會將 block 以外的任何 props 傳遞給已註冊的元件,因此頁面會將 story UUID 以 storyUuid prop 向下傳遞。

建立該區塊的元件:

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 中註冊它。鍵值必須與區塊的技術名稱一致:

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 欄位中的區塊:

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 元件及其 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

Storyblok 的 Next.js 指南中的 page 元件已經渲染了 <main> 元素,因此路由可以直接回傳 StoryblokBlock 而不需要包裝元素。

如果 FastComments 區塊被放置在另一個區塊內,例如網格,該區塊的元件也必須將 storyUuid 傳遞給其巢狀的 StoryblokBlock 元素。否則 FastComments 區塊將收不到 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。

對於 EU 區域的帳戶,請從 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 函式庫時,設定選項會以 props 的形式傳遞給 FastComments 元件。使用 VanillaJS 腳本時,則是將選項加入傳遞給 window.FastCommentsUI 的物件中。許多選項也可以在您 FastComments 帳戶的小工具自訂頁面上,不需撰寫程式碼即可修改。

選項也可以來自 FastComments 區塊。例如,在區塊庫中新增至 fastcomments 區塊的布林欄位,會出現在 FastCommentsBlock 的 block prop 上,並可傳遞給小工具,例如對於技術名稱為 readonly 的欄位,可使用 readonly={block.readonly}。請參閱防止新回覆並停用投票。

若要將您網站的登入使用者顯示為留言作者,請使用 FastComments 安全 SSO。SSO 驗證雜湊是使用您的 FastComments API 密鑰計算的 HMAC-SHA256 雜湊,因此必須在伺服器端程式碼中產生,例如 Next.js 伺服器元件,然後傳遞給小工具。請勿在瀏覽器 JavaScript 中計算,也不要將 API 密鑰包含在用戶端程式碼中。