# 상세 블록 렌더링

원문: https://commerce.bootpay.ai/api/catalog/block-rendering

> 상품 상세의 블록 HTML과 본문 중간 이미지를 Next.js에서 안전하게 표시하는 방법

v1 API 상세: [상품](https://commerce.bootpay.ai/api/reference/v1/products)

[상품 상세](https://commerce.bootpay.ai/api/catalog/detail)의 `content`에는 판매자가 저장한 **완성된 상세 HTML**이 들어 있습니다. 사진을 표시할 때 `images`나 `content_images`만 읽으면 블록 본문 중간의 사진이 빠집니다. `content`를 일반 문자열로 표시하거나 HTML 태그를 모두 제거하면 사진·표·문답·글 배치가 사라집니다.

쇼핑몰은 **`content`를 안전하게 렌더링하고, 현재 블록 CSS를 함께 적용**합니다. 상품 조회 화면을 만들 때는 `content_blocks`로 편집기나 컴파일러를 다시 만들 필요가 없습니다.

## API 필드와 우선순위

| 필드 | 공개 응답 타입 | 역할 |
| --- | --- | --- |
| `images` | `String[]` | 상단 대표 이미지·갤러리. 상세 본문을 대신하지 않습니다. |
| `desc` | `String` | 상품 요약. 상세 본문과 별도입니다. |
| `content` | `String` | 블록/HTML 편집 결과. 글·이미지·표의 표시 순서가 이미 들어 있습니다. |
| `content_type` | `String` | `block` 또는 `image`. 렌더 우선순위를 이 필드만으로 결정하면 안 됩니다. |
| `content_blocks` | `Object[]` | 편집 원본. 블록마다 `id`, `type`과 유형별 값이 있습니다. 상세 응답에 포함될 수 있으며 목록에서는 보장하지 않습니다. |
| `content_images` | `String[]` | 이미지 편집 방식의 상세 이미지. `content`가 없을 때 배열 순서로 표시합니다. |

1. 안전하게 변환한 `content`에 표시할 글이나 이미지가 있으면 그대로 표시합니다.
2. `content`가 없거나 표시 가능한 요소가 하나도 없으면 `content_images`를 순서대로 표시합니다.
3. 둘 다 없으면 등록된 상품 요약이나 빈 상세 안내를 보여줍니다.

`content`와 `content_images`를 무조건 합치면 안 됩니다. 전환 전 이미지가 남아 있으면 같은 상품 사진이 중복되거나 판매자가 정한 순서가 바뀝니다. `content_images=[]`여도 HTML 안에 사진이 있으면 정상 상세입니다.

## 블록 원본과 완성 HTML

다음은 구조를 보여주는 축약 예시입니다. 실제 상품 사진은 API가 반환하는 절대 HTTPS URL을 그대로 사용합니다.

```json
{
  "product_id": "67c95e64d01640bb9859c629",
  "content_type": "block",
  "content_blocks": [
    { "id": "intro", "type": "text", "html": "소재와 사용 방법을 소개해요." },
    { "id": "photo", "type": "image", "items": [{ "url": "https://example.com/product-middle.jpg", "alt": "상품의 옆면" }], "ratio": "origin", "layout": "stack" },
    { "id": "outro", "type": "text", "html": "부드러운 천으로 닦아 주세요." }
  ],
  "content": "<div class=\"bp-detail\"><div class=\"bp-text description-text\">소재와 사용 방법을 소개해요.</div><div class=\"bp-images bp-ratio-origin bp-images-stack\"><figure class=\"bp-image\"><img src=\"https://example.com/product-middle.jpg\" alt=\"상품의 옆면\" loading=\"lazy\" style=\"max-width:100%;height:auto\"></figure></div><div class=\"bp-text description-text\">부드러운 천으로 닦아 주세요.</div></div>",
  "content_images": []
}
```

블록 저장 시 서버 컴파일러가 원본 배열 순서대로 HTML을 만들고 `.bp-detail`로 감쌉니다. `{{상품명}}`, `{{판매가}}`, `{{배송비}}` 토큰과 공용 안내 블록의 `sharedId`는 서버가 저장하거나 재컴파일할 때 해석합니다. `subscriptionPlan`, `policyBadges`는 상품·배송 정책 문맥으로 만들어집니다. 쇼핑몰에서 블록 원본만 다시 조립하면 이 값이 누락되거나 표시 내용이 달라질 수 있습니다.

## CSS와 이미지 자산

완성 HTML 안쪽 `.bp-detail` 바깥에 **`.bp-detail-view.is-responsive`**를 추가합니다. CSS를 빼면 사진만 보이고 커버, 좌우 배치, 이미지 그리드, 표, 배경 면의 모양은 달라집니다.

공용 CSS 는 두 파일로 제공됩니다.

- `stylesheets/product-detail.css`
- `stylesheets/product-detail.contract.json`

Next.js 앱에는 두 파일을 **같은 스냅샷으로** `src/vendor/product-detail/`에 포함하고 아래 컴포넌트에서 CSS를 import합니다. 별도의 이미지 리소스 묶음은 필요 없습니다. 상품 사진은 API URL을 사용하고 내장 라인 아이콘은 CSS의 SVG mask에 들어 있습니다. 웹폰트는 쇼핑몰의 기존 폰트 정책을 따릅니다.

::: warning CSS 배포 경로를 확인합니다
`@bootpay/product-detail-css@1.0.0` 패키지는 최신 블록(커버·확장 블록)을 포함하지 않으므로 사용하면 안 됩니다. 현재 CSS 와 계약 JSON 은 Bootpay 에 요청해 받아 앱에 포함합니다. 두 파일 없이 JSX 만으로는 관리자 미리보기와 같은 상세를 보장할 수 없습니다.
:::

래퍼에 `bp-detail-view`만 쓰면 좁은 화면 모양으로, `is-pc`를 추가하면 PC 모양으로 고정됩니다. **실제 쇼핑몰은 `is-responsive`**를 써서 768px 이상에서 PC 배치를 적용합니다. 판매자 테마/색상 설정을 연결할 때는 바깥 래퍼의 `bp-theme-editorial`, `bp-theme-mono`와 `--bp-detail-link`, `--bp-detail-radius-scale`, `--bp-detail-scale`만 검증 후 조정합니다. 내부 블록 순서나 원본 사진 URL을 변경하면 안 됩니다. `imageText.align`의 `left/right`는 공간이 있으면 좁은 화면에서도 좌우를 유지하고 부족하면 줄바꿈합니다. 사진 위·글 아래를 지정한 `top`은 세로 배치입니다. 모든 블록이 모바일에서 무조건 세로로 바뀐다고 가정하면 안 됩니다.

## Next.js 서버에서 표시 필드만 전달하기

Node.js SDK는 서버에서만 생성합니다. 아래 예제는 `@bootpay/backend-js@2.16.0`의 실제 `product.productDetail(productId)` 호출과 감싸지 않은 응답을 기준으로 합니다.

```ts
// src/lib/product-detail.ts — 서버 전용
import 'server-only';
import { BootpayCommerce } from '@bootpay/backend-js';

const commerce = new BootpayCommerce({
  client_key: process.env.BOOTPAY_CLIENT_KEY!,
  secret_key: process.env.BOOTPAY_SECRET_KEY!,
  mode: 'production',
});

export async function getProductDetailBody(productId: string, projectId: string) {
  // SDK 응답 타입(CommerceProduct)에는 desc·content 같은 서버 필드가 빠져 있습니다.
  // 운영의 raw JSON을 unknown으로 받아 검사하고 SDK DTO에 맞춰 캐스팅하지 않습니다.
  const response: unknown = await commerce.product.productDetail(productId);
  if (!response || typeof response !== 'object' || Array.isArray(response)) {
    throw new Error('상품 상세 응답을 확인할 수 없어요.');
  }
  const product = response as Record<string, unknown>;
  if (product.project_id !== projectId || product.product_id !== productId) {
    throw new Error('상품 소속을 확인할 수 없어요.');
  }
  if (typeof product.name !== 'string') throw new Error('상품명을 확인할 수 없어요.');
  return {
    name: product.name,
    description: typeof product.desc === 'string' ? product.desc : '',
    detailHtml: typeof product.content === 'string' ? product.content : '',
    detailImages: Array.isArray(product.content_images)
      ? product.content_images.filter((value: unknown): value is string => typeof value === 'string')
      : [],
  };
}
```

`projectId`는 인증된 서버 설정/프로젝트 범위 목록에서 얻습니다. 브라우저가 보낸 값을 그대로 신뢰하면 안 됩니다. 상세 응답 전체를 BFF에서 반환하면 디지털 지급 파일·접근 코드 등 서버 전용 데이터까지 전달될 수 있습니다. 상품명·요약·완성 HTML·공개 이미지 등 **허용한 표시 필드만** 반환합니다. `content_blocks`는 이 렌더 방식에 필요하지 않습니다. 전체 상품 상세 API의 오류·권한 처리는 [상품 상세](https://commerce.bootpay.ai/api/catalog/detail)를 따릅니다.

## 안전한 React 렌더러 예제

서버가 만든 `content`도 렌더 경계에서 검사합니다. 특히 `html`의 `source:"legacy"`는 과거 원본을 그대로 보존한 경우가 있으므로, 서버가 저장할 때 모든 HTML을 완전히 정제했다고 가정하면 안 됩니다. 정규식으로 태그를 제거한 뒤 `dangerouslySetInnerHTML`에 넣는 방식은 쓰면 안 됩니다.

다음 구현은 브라우저의 **분리된 `<template>`**로 파싱한 뒤, 허용 태그·속성·계약 클래스만 React 요소로 새로 만듭니다. 원본 DOM을 페이지에 붙이지 않으며 `script`, 이벤트 속성, 임의 style, iframe, form, SVG/MathML, 외부 클래스는 전달하지 않습니다. 이미지는 HTTPS URL만, 링크는 HTTPS 새 창과 `noopener noreferrer`로 제한합니다. 상대 URL은 어느 서버의 경로인지 불명확하므로 이 예제는 허용하지 않습니다. 상대 이미지가 필요한 연동은 인증 쿠키가 닿지 않는 정적 호스트를 명시하고 URL을 검증하는 계약부터 정합니다.

아래 파일과 `product-detail-body.css`, 앞에서 설명한 vendor 두 파일을 같은 경로에 넣습니다.

```tsx
"use client";

import { createElement, useMemo, useState, useSyncExternalStore, type ReactNode } from "react";
import contract from "@/vendor/product-detail/product-detail.contract.json";
import "@/vendor/product-detail/product-detail.css";
import "./product-detail-body.css";

type Product = {
  name: string;
  description?: string;
  detailHtml: string;
  detailText?: string;
  detailImages: string[];
};

function imageUrl(value: unknown): string | null {
  if (typeof value !== 'string') return null;
  try {
    const url = new URL(value);
    return url.protocol === 'https:' && !url.username && !url.password ? url.href : null;
  } catch { return null; }
}

const tags = new Set("p div section span br b strong i em u s a ul ol li h2 h3 h4 h5 h6 table caption thead tbody tfoot tr th td figure figcaption img blockquote hr details summary".split(" "));
const classes = new Set([...Object.keys(contract.classes), ...Object.keys(contract.legacyAliases)]);
const subscribe = () => () => {};
const browserSnapshot = () => true;
const serverSnapshot = () => false;

function DetailImage({ src, alt, className }: { src: string; alt: string; className?: string }) {
  const [failed, setFailed] = useState(false);
  if (failed) return <span className="detail-image-unavailable" role="status">{alt ? `${alt} — ` : ""}이미지를 불러오지 못했어요.</span>;
  return <img src={src} alt={alt} className={className} loading="lazy" decoding="async" referrerPolicy="no-referrer" onError={() => setFailed(true)} />;
}

/**
 * Parse into an inert template, then rebuild permitted HTML as React elements.
 * Never attach the parsed DOM or spread its attributes onto a live element.
 * Server HTML is not assumed safe: events, arbitrary CSS, embeds, forms, SVG,
 * scripts, relative URLs and unsupported tags/attributes never reach the page.
 */
function renderDetailHtml(html: string, productName: string): ReactNode[] {
  if (!html || html.length > 1_000_000) return [];
  const template = document.createElement("template");
  template.innerHTML = html;
  let visited = 0;
  let meaningful = false;
  function walk(node: Node, key: string, depth: number): ReactNode {
    if (++visited > 12000 || depth > 64) throw new Error("Detail content is too complex");
    if (node.nodeType === Node.TEXT_NODE) {
      if (node.textContent?.trim()) meaningful = true;
      return node.textContent;
    }
    if (node.nodeType !== Node.ELEMENT_NODE) return null;
    const element = node as Element;
    const tag = element.localName;
    if (element.namespaceURI !== "http://www.w3.org/1999/xhtml" || !tags.has(tag)) return null;
    // Compiled meter dots use this one state class outside the bp-* contract.
    const meterDot = tag === "i" && element.parentElement?.classList.contains("bp-meter__scale");
    const className = (element.getAttribute("class") || "").split(/\s+/).filter(name => classes.has(name) || (meterDot && name === "is-on")).join(" ");
    if (tag === "img") {
      const src = imageUrl(element.getAttribute("src"));
      if (src) meaningful = true;
      return src ? <DetailImage key={key} src={src} alt={element.getAttribute("alt") || `${productName} 상세 이미지`} className={className} /> : null;
    }
    const props: Record<string, unknown> = { key, ...(className ? { className } : {}) };
    if (element.hasAttribute("title")) props.title = element.getAttribute("title");
    if (element.getAttribute("aria-hidden") === "true") props["aria-hidden"] = true;
    if (element.hasAttribute("aria-label")) props["aria-label"] = element.getAttribute("aria-label");
    if (element.getAttribute("role") === "img") props.role = "img";
    if (tag === "a") {
      const href = imageUrl(element.getAttribute("href"));
      if (href) Object.assign(props, { href, target: "_blank", rel: "noopener noreferrer" });
    }
    if (tag === "th" || tag === "td") {
      for (const [attribute, property] of [["colspan", "colSpan"], ["rowspan", "rowSpan"]]) {
        const value = Number(element.getAttribute(attribute));
        if (Number.isInteger(value) && value > 0 && value <= 100) props[property] = value;
      }
      const scope = element.getAttribute("scope");
      if (scope && ["row", "col", "rowgroup", "colgroup"].includes(scope)) props.scope = scope;
    }
    if (tag === "details" && element.hasAttribute("open")) props.open = true;
    if (tag === "br" || tag === "hr") return createElement(tag, props);
    const children = Array.from(node.childNodes).map((child, index) => walk(child, `${key}.${index}`, depth + 1));
    return createElement(tag, props, ...children);
  }
  try {
    const content = Array.from(template.content.childNodes).map((node, index) => walk(node, `${index}`, 0));
    return meaningful ? content : [];
  } catch {
    return [];
  }
}

export function ProductDetailBody({ product }: { product: Product }) {
  const browserReady = useSyncExternalStore(subscribe, browserSnapshot, serverSnapshot);
  const content = useMemo(() => browserReady ? renderDetailHtml(product.detailHtml || "", product.name) : [], [browserReady, product.detailHtml, product.name]);
  if (!browserReady) return <p className="detail-body-status" role="status">상세 설명을 불러오고 있어요.</p>;
  // Compiled content wins even when a legacy content_type still says "image".
  // Appending content_images would duplicate or reorder the merchant's layout.
  return <div className="bp-detail-view is-responsive storefront-detail-body">
    {content.length > 0 ? content : product.detailImages.length > 0 ? <div className="bp-detail"><div className="bp-images bp-ratio-origin bp-images-stack">{product.detailImages.filter(src => imageUrl(src)).map((src, index) => <figure className="bp-image" key={`${src}-${index}`}><DetailImage src={src} alt={`${product.name} 상세 이미지 ${index + 1}`} /></figure>)}</div></div> : <p className="detail-body-status">{product.detailText || product.description || "상세 설명이 아직 등록되지 않았어요. 상품 정보는 판매자에게 문의해 주세요."}</p>}
  </div>;
}

```

`detailHtml`을 통째로 텍스트로 바꾸지 않으므로 본문 중간 이미지를 같은 위치에 유지합니다. React가 글자·속성 값을 escape하며, 이미지가 실패하면 해당 자리에 안내를 보여줍니다. 브라우저 DOM을 쓰므로 이 예제는 클라이언트 컴포넌트입니다. SSR 본문이 필요한 서비스는 검증된 서버 DOM 파서/정제기를 별도로 선택하고 동일한 태그·속성·클래스·URL 계약으로 검사합니다. 브라우저 파서를 서버에서 그대로 호출하면 안 됩니다.

```css
.storefront-detail-body {
  min-width: 0;
  text-align: left;
  overflow-wrap: anywhere;
  --bp-detail-link: #565f42;
}
.storefront-detail-body img { display: block; max-width: 100%; height: auto; }
.storefront-detail-body figure { margin: 0; }
.storefront-detail-body table { max-width: 100%; }
.detail-body-status { color: #73766e; line-height: 1.9; white-space: pre-line; }
.detail-image-unavailable { display: block; padding: 30px 16px; color: #73766e; text-align: center; }

```

넓은 상세 영역에 컴포넌트를 배치합니다. 상단 상품 안내 아코디언에는 요약을, 전체 상세에는 사진·표가 들어 있는 본문을 배치하면 긴 내용을 좁은 구매 패널 안에 넣지 않아도 됩니다.

```tsx
<section aria-labelledby="product-detail-heading">
  <h2 id="product-detail-heading">상품 상세 설명</h2>
  <ProductDetailBody product={detail} />
</section>
```

## 블록 유형별 표시 계약

지원하는 블록 유형은 18개입니다. 아래는 표시 기준이며, 상품 등록·수정 API 가 모든 블록 원본의 쓰기를 지원하지는 않습니다. 각 유형의 세부 필드 전체를 새 렌더러로 재구현하기보다 완성 HTML과 공용 CSS를 사용합니다.

| `type` | 편집 원본의 주요 값 | 완성 HTML·표시 포인트 |
| --- | --- | --- |
| `heading` | `text`, `html`, `level`, `size`, `eyebrow` | `h2/h3.bp-section-title`, 제목 크기·위 라벨 |
| `text` | `html`, `emphasis` | `.bp-text`, 줄바꿈·강조·인용·인라인 글자 크기 |
| `image` | `items[{url,alt,caption}]`, `ratio`, `layout` | `.bp-images`, `figure/img/figcaption`, stack/grid2/grid3와 원본 순서 |
| `imageText` | `image`, `html`, `align`, `ratio`, `imageWidth`, `verticalAlign` | `.bp-image-text`, 사진 왼쪽/오른쪽/위, 폭에 따른 줄바꿈 |
| `features` | `items[{icon,title,text}]`, `preset` | `.bp-features`, 특징 목록·번호/아이콘/구분선 |
| `specs` | `rows[{label,value}]`, `columns` | `.bp-specs`, `table/tbody/tr/th/td` |
| `notice` | `title`, `html`, `tone`, `sharedId` | `.bp-notice`, 공용 안내는 서버에서 해석 |
| `html` | `raw`, `source` | `.bp-html`, legacy도 렌더 경계 검사 필수 |
| `subscriptionPlan` | `source:"product"` | `.bp-subscription-plan`, 상품 회차에서 생성된 정적 안내 표 |
| `cover` | `image`, `title`, `text`, `pos`, `dim`, `height`, `eyebrow` | `.bp-cover`, 커버 사진·명암·문구 위치 |
| `steps` | `items[{title,text}]`, `preset` | `.bp-steps`, 순서가 있는 안내 |
| `stats` | `items[{value,label,unit}]` | `.bp-stats`, 핵심 수치와 단위 |
| `faq` | `items[{q,a}]` | `.bp-faq`, 네이티브 `details/summary` 접기·펼치기 |
| `compare` | `headers`, `rows` | `.bp-compare`, 비교 표와 표 머리글 |
| `policyBadges` | `show` | `.bp-policy-badges`, 연결 배송 정책에서 생성된 배지 |
| `sizeGuide` | `columns`, `rows[{label,values}]`, `note` | `.bp-size-guide`, 실측 표와 오차 안내 |
| `meterGroup` | `items[{label,level,low,high}]` | `.bp-meters`, 정적 수준 표시·접근성 라벨 |
| `fitFor` | `good`, `bad` | `.bp-fit-for`, 추천 대상·유의 사항 |

`spacing`, `section`, `sectionPad`, `sectionAlign`, `bleed` 같은 공통 값은 블록 간격·면 배경·폭에 영향을 줍니다. 이미지의 `focus`는 자르기 기준점입니다. `originUrl`, `hint` 등 편집 보조 값으로 현재 이미지 URL을 교체하거나 화면 문구를 만들면 안 됩니다. 같은 `section`이 연속된 블록은 하나의 section으로 컴파일될 수 있습니다.

## 로딩·오류·검증

상품 API를 불러오는 동안 로딩 상태를 보여주고, 404는 상품 없음, 통신 오류는 다시 시도 상태로 처리합니다. 본문을 못 읽었다는 이유로 다른 상품 사진이나 가짜 설명을 채우면 안 됩니다. 위 렌더러는 읽을 수 없거나 지나치게 큰 HTML 대신 상세 이미지나 빈 상태를 보여 주며, 최대 1,000,000자·12,000노드·64단계의 파싱 한도를 둡니다. 이 한도는 위 예제 렌더러의 방어 기준이며 API의 저장 상한이 아닙니다.

구현 후 아래를 실제 브라우저에서 검증합니다.

- `text → imageText → text → image` 순서와 본문 중간 사진을 유지하는지 확인합니다.
- HTML+`content_images`가 함께 있을 때 중복 표시가 없는지, 이미지 방식과 빈 상세가 올바른지 확인합니다.
- 데스크톱/모바일에서 판매자가 지정한 좌우·상하 배치와 줄바꿈, 원본 비율·표·FAQ와 가로 넘침을 확인합니다.
- 이미지 로딩 실패 안내, `alt`, 문답 키보드 조작과 링크 정책을 확인합니다.
- `script`, `onerror`, `javascript:`/`data:` URL, 외부 CSS/클래스, iframe, 폼, SVG/MathML 입력이 실행되거나 삽입되지 않는지 확인합니다.
- CSS와 계약 JSON을 함께 갱신하고, API의 새 블록 클래스가 정제 과정에서 빠지지 않는지 확인합니다.

## 함께 보기

- [상품 상세](https://commerce.bootpay.ai/api/catalog/detail) — SDK 호출·응답·권한 경계
- [상품 목록](https://commerce.bootpay.ai/api/catalog/list) — 대표 이미지·목록과 상세 데이터의 차이
- [상품 옵션](https://commerce.bootpay.ai/api/catalog/categories) — 구매 옵션 표시와 주문 요청
