---
title: "框架接入与 SSR"
description: "在 React、Vue 和 Svelte 中使用同一个组件，并为服务端渲染提供可读的初始讨论串。"
---

> Documentation Index
> Fetch the complete documentation index at: https://blua.pwp.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# 框架接入与 SSR

## 安装 SDK

在 Blua 仓库中打包。将生成的 `.tgz` 交给宿主项目，然后在宿主项目中从实际路径安装。下例假设文件已复制到宿主项目的 `vendor` 目录。

```sh
pnpm sdk:pack
```

`packages/embed/dist/blua-embed-0.0.0.tgz`

```sh
pnpm add ./vendor/blua-embed-0.0.0.tgz
```

## React

```tsx
import { BluaComments } from '@blua/embed/react';

export function Discussion({ slug }: { slug: string }) {
  return (
    <BluaComments endpoint="https://blua.example" siteKey="garden" pageKey={slug} locale="zh" />
  );
}
```

## Vue

```vue
<script setup lang="ts">
import { BluaComments } from '@blua/embed/vue';
defineProps<{ slug: string }>();
</script>

<template>
  <BluaComments endpoint="https://blua.example" site-key="garden" :page-key="slug" locale="zh" />
</template>
```

## Svelte

```svelte
<script lang="ts">
  import BluaComments from '@blua/embed/svelte';
  let { slug }: { slug: string } = $props();
</script>

<BluaComments endpoint="https://blua.example" siteKey="garden" pageKey={slug} locale="zh" />
```

## SSR

在服务端读取公开的 bootstrap，并作为 `bootstrap` 属性交给适配器。读者收到的 HTML 包含评论列表；组件在浏览器端接管并复用这份数据。下面的函数适合自己输出 HTML 的服务端模板。

```ts
import { createBluaApiClient, renderBluaCommentsSsr } from '@blua/embed';

export async function renderDiscussion(slug: string) {
  const props = { endpoint: 'https://blua.example', siteKey: 'garden', pageKey: slug };
  const bootstrap = await createBluaApiClient(props).bootstrap('newest');
  return renderBluaCommentsSsr({ ...props, bootstrap, locale: 'zh' });
}
```

在页面中加载网关的 `embed.js` 以启用交互。公共缓存只应包含公开评论。作者可见的待审内容、账号会话和站点凭据须留在私有响应中。

Source: https://blua.pwp.sh/docs/embed/frameworks/
