CWD 댓글 시스템 통합

CWD 댓글 시스템 통합 가이드

창업

CWD 댓글 시스템 통합 가이드

Astro 프로젝트를 초기화하고 CWD 댓글 시스템을 통합하는 데 성공했습니다.

주요 변경사항

  1. 프로젝트 초기화: basics 템플릿을 사용하여 새 Astro 프로젝트를 생성했습니다.
  2. 컴포넌트 생성: 재사용 가능한 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>
  1. 페이지 통합: 댓글 컴포넌트를 홈페이지 [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이 올바르게 설정되었는지 확인):

Terminal window
npm run dev

다른 Astro 프로젝트에서 재사용하기

이 댓글 시스템을 다른 Astro 프로젝트에서 사용하려면 백엔드나 관리 패널을 다시 배포할 필요가 없습니다. 프론트엔드 컴포넌트만 복사하세요.

  1. 컴포넌트 복사: [src/components/CwdComments.astro]를 새 프로젝트의 src/components/ 디렉터리에 복사하세요. 참고: 프로젝트에서 View Transitions를 사용하는 경우 registerPageInit 로직을 유지하세요.

  2. 페이지에 추가: 페이지에서 가져와서 사용하세요:

    ---
    import CwdComments from '../components/CwdComments.astro';
    ---
    <CwdComments apiBaseUrl="https://cmt-api.xxx.com" />
  3. 고유 식별: 시스템은 페이지 URL(경로)에 따라 자동으로 댓글을 구분합니다. 새 프로젝트의 도메인이나 경로가 다륻면 댓글이 자동으로 격리됩니다.

최신 상태 유지하기

CWD는 빈번한 업데이트가 있는 초기 개발 단계이므로 다음 업데이트 전략을 따르세요:

  1. 백엔드 (API):

    • cwd 디렉터리에서 git fetch upstreamgit merge upstream/main을 실행하여 코드를 동기화하세요.
    • npm install을 실행하여 종속성을 업데이트하세요.
    • npx wrangler d1 migrations apply CWD_DB를 실행합니다(데이터베이스 변경사항이 있는 경우).
    • npm run deploy를 실행하여 새 버전의 Worker를 배포하세요.
  2. 프론트엔드 컴포넌트:

    • src/components/widgets/CwdComments.astro에서 스크립트 URL 버전을 업데이트하세요:
      <script is:inline src="https://unpkg.com/cwd-widget@latest/dist/cwd.js"></script>
    • 최신 버전을 가져오려면 @latest를 사용하세요(개발 시 권장), 또는 안정성을 위해 버전 번호(예: @0.0.5)를 지정하세요.
  3. 관리 패널:

    • 최신 코드를 가져옵니다(백엔드와 동일한 단계).
    • npm installnpm run build를 실행하세요.
    • Cloudflare Pages에 다시 배포하세요: npx wrangler pages deploy dist --project-name cwd-admin.

게시일: 2026년 2월 6일 · 수정일: 2026년 2월 6일

관련 게시물