CWD コメントシステム統合ガイド
Astroプロジェクトを初期化し、CWDコメントシステムを統合することに成功しました。
主な変更点
- プロジェクト初期化:
basicsテンプレートを使用して新しいAstroプロジェクトを作成。 - コンポーネント作成: 再利用可能なAstroコンポーネント [src/components/CwdComments.astro] を作成。
---interface Props { apiBaseUrl: string; elId?: string; pageSize?: number; theme?: 'light' | 'dark';}const { apiBaseUrl, elId = 'comments', pageSize = 20, theme = 'light' } = Astro.props;---
<!-- 1. コンテナ:data属性を使用してスクリプトにパラメータを渡す --><div id={elId} class="cwd-comments-container" data-el-id={elId} data-api-base-url={apiBaseUrl} data-page-size={pageSize} data-theme={theme}></div><!-- 2. ライブラリのインポート:いつでも読み込めるようis:inlineを保持 --><script is:inline src="https://unpkg.com/cwd-widget@latest/dist/cwd.js"></script><!-- 3. 初期化スクリプト:ユーティリティ関数をインポートするためにモジュールを使用 --><script> import { registerPageInit } from '@/utils/page-init'; // 初期化ロジックを定義 const initComments = () => { const containers = document.querySelectorAll('.cwd-comments-container');
containers.forEach((container) => { const el = container as HTMLElement; // 重複初期化を防止 if (el.children.length > 0 || el.dataset.cwdInitialized) return; const apiBaseUrl = el.dataset.apiBaseUrl; const theme = el.dataset.theme; const pageSize = Number(el.dataset.pageSize);
// ライブラリの読み込みを確認 if ((window as any).CWDComments && apiBaseUrl) { try { const comments = new (window as any).CWDComments({ el: el, apiBaseUrl, pageSize, theme, }); comments.mount(); // 初期化済みとしてマーク el.dataset.cwdInitialized = 'true'; } catch (e) { console.error('[CWD] 初期化失敗:', e); } } else { // スクリプトがまだ読み込み中の場合、簡易的にポーリング const checkTimer = setInterval(() => { if ((window as any).CWDComments) { clearInterval(checkTimer); // 再トリガー initComments(); } }, 100); // 5秒後にタイムアウト停止 setTimeout(() => clearInterval(checkTimer), 5000); } }); // クリーンアップ関数を返す(オプション) return () => { // CWDはdestroyメソッドを提供していない可能性があるので、コンテナを空にするだけ }; }; // 4. PageInitManagerに登録 registerPageInit('init-cwd-comments', initComments, { immediate: true });</script>- ページ統合: コメントコンポーネントをホームページ [src/pages/index.astro] に追加。
設定方法
コメントシステムはバックエンドに接続するためのAPI URLを必要とします。[src/pages/index.astro](またはこのコンポーネントを使用する場所)のapiBaseUrl属性を更新してください。
ファイル: [src/pages/index.astro]
<CwdComments apiBaseUrl="https://cmt-api.xxx.com" />https://cmt-api.xxx.comを実際にデプロイされたWorkerのURLに置き換えてください。
コンポーネントの使用方法
CwdCommentsコンポーネントは任意のAstroページまたはレイアウトで使用できます:
---import CwdComments from '../components/CwdComments.astro';---
<CwdComments apiBaseUrl="https://cmt-api.xxx.com" elId="comments" theme="light"/>検証
プロジェクトは正常にビルドされます。コメントコンポーネントを確認するには開発サーバーを実行してください(API URLが正しく設定されていることを確認):
npm run dev他のAstroプロジェクトでの再利用
このコメントシステムを他のAstroプロジェクトで使用する場合、バックエンドや管理パネルを再デプロイする必要はありません。フロントエンドコンポーネントをコピーするだけです。
-
コンポーネントのコピー: [src/components/CwdComments.astro] を新しいプロジェクトの
src/components/ディレクトリにコピーしてください。 注意:プロジェクトがView Transitionsを使用している場合は、registerPageInitロジックを保持してください。 -
ページへの追加: ページでインポートして使用します:
---import CwdComments from '../components/CwdComments.astro';---<CwdComments apiBaseUrl="https://cmt-api.xxx.com" /> -
一意の識別: システムはページURL(パス)によって自動的にコメントを区別します。新しいプロジェクトのドメインまたはパスが異なる限り、コメントは自動的に分離されます。
最新状態の維持
CWDは早期開発段階にあり頻繁に更新されるため、以下の更新戦略に従ってください:
-
バックエンド(API):
cwdディレクトリでgit fetch upstreamとgit merge upstream/mainを実行してコードを同期。npm installを実行して依存関係を更新。npx wrangler d1 migrations apply CWD_DBを実行(データベースの変更がある場合)。npm run deployを実行して新しいバージョンのWorkerをデプロイ。
-
フロントエンドコンポーネント:
src/components/widgets/CwdComments.astroでスクリプトURLのバージョンを更新:<script is:inline src="https://unpkg.com/cwd-widget@latest/dist/cwd.js"></script>- 最新版を取得するには
@latestを使用(開発時に推奨)、または安定性のためバージョン番号(例:@0.0.5)を指定。
-
管理パネル:
- 最新コードをプル(バックエンドと同じ手順)。
npm installとnpm run buildを実行。- Cloudflare Pagesに再デプロイ:
npx wrangler pages deploy dist --project-name cwd-admin。
公開日: 2026年2月6日 · 更新日: 2026年2月6日