Skip to content

Commit 9d5aff9

Browse files
committed
feat(scripts): 新增本地 DocSearch 索引生成脚本,Mermaid 组件增加加载失败提示
- 新增 scripts/docsearch-index.mjs,支持从本地 dist 生成 Algolia 索引 - package.json 添加 docsearch:index 命令 - PERFORMANCE_NOTES.md 补充 Algolia 新应用配置和索引流程说明 - LazyMermaid.vue 增加 Mermaid 加载失败的错误处理和提示
1 parent fd6f0e0 commit 9d5aff9

4 files changed

Lines changed: 391 additions & 8 deletions

File tree

PERFORMANCE_NOTES.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,42 @@ cp "$VERIFY_FILE" "$SITE_DIR/"
9292
- `DOCSEARCH_INDEX_NAME`
9393
- 没有 DocSearch key 时关闭搜索,避免生成本地 `searchIndex.js`
9494
- clean build 后已确认 `docs/.vuepress/.temp/internal/searchIndex.js` 不再生成。
95+
- Algolia 应用:
96+
- 当前新应用 ID:`XXQ4GI90SC`
97+
- 当前前端索引名:`javaguide`
98+
- 当前前端 Search-Only API Key 已验证可用,掩码记录为:`3b514f...ef027b`
99+
- 官方 DocSearch Crawler 当前存在抽取不稳定问题:
100+
- Crawler 能访问页面,但 UI 中 `recordExtractor` 没有稳定产出 records。
101+
- 线上连续抓取还可能受 CDN/安全策略影响,导致部分页面拿不到完整正文。
102+
- 新增兜底索引脚本:`pnpm docsearch:index`
103+
- 脚本位置:`scripts/docsearch-index.mjs`
104+
- 推荐从本地构建产物 `dist` 生成索引,而不是在线抓取。
105+
- 原因:`dist` 就是最终部署产物,索引内容和发布内容一致,也不会受 CDN/反爬/缓存影响。
106+
- 推荐流程:
107+
108+
```bash
109+
pnpm docs:build
110+
111+
DOCSEARCH_APP_ID=XXQ4GI90SC \
112+
DOCSEARCH_INDEX_NAME=javaguide \
113+
DOCSEARCH_SOURCE_DIR=dist \
114+
DOCSEARCH_ADMIN_API_KEY=你的写入索引专用 Key \
115+
pnpm docsearch:index
116+
```
117+
118+
- 注意:
119+
- `DOCSEARCH_ADMIN_API_KEY` 只用于本地/CI 写索引,不能提交到仓库,不能放到前端环境变量里。
120+
- 前端 `DOCSEARCH_API_KEY` 必须使用 `XXQ4GI90SC` 应用下的 Search-Only API Key,不能继续用旧应用 `U3RN7F5WI0` 的 key。
121+
- 前端本地/部署构建环境变量示例:
122+
123+
```bash
124+
DOCSEARCH_APP_ID=XXQ4GI90SC
125+
DOCSEARCH_INDEX_NAME=javaguide
126+
DOCSEARCH_API_KEY=3b514f...ef027b
127+
```
128+
129+
- 上面的 `DOCSEARCH_API_KEY` 文档中只保留掩码;实际构建时使用完整 Search-Only API Key。
130+
- 2026-05-14 已用本地 `dist` 成功写入 `javaguide` 索引,索引 records 约 4.7 万条。
95131

96132
### GlobalUnlock
97133

docs/.vuepress/components/LazyMermaid.vue

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
:code="code"
66
:title="title"
77
/>
8-
<div v-else ref="placeholderEl" class="mermaid-lazy-placeholder">
9-
<span class="mermaid-lazy-spinner" aria-hidden="true" />
10-
<span>图表加载中</span>
8+
<div
9+
v-else
10+
ref="placeholderEl"
11+
class="mermaid-lazy-placeholder"
12+
:class="{ 'is-error': loadError }"
13+
>
14+
<span v-if="!loadError" class="mermaid-lazy-spinner" aria-hidden="true" />
15+
<span>{{ loadError ?? "图表加载中" }}</span>
1116
</div>
1217
</template>
1318

@@ -23,16 +28,22 @@ defineProps<{
2328
const placeholderEl = shallowRef<HTMLElement | null>(null);
2429
const shouldRender = shallowRef(false);
2530
const MermaidComponent = shallowRef<Component | null>(null);
31+
const loadError = shallowRef<string | null>(null);
2632
let observer: IntersectionObserver | null = null;
2733
2834
const loadMermaidComponent = async () => {
2935
if (MermaidComponent.value) return;
3036
31-
const { default: Mermaid } = await import(
32-
/* @vite-ignore */
33-
"@vuepress/plugin-markdown-chart/client/components/Mermaid.js"
34-
);
35-
MermaidComponent.value = markRaw(Mermaid);
37+
try {
38+
const { default: Mermaid } = await import(
39+
"@vuepress/plugin-markdown-chart/client/components/Mermaid.js"
40+
);
41+
MermaidComponent.value = markRaw(Mermaid);
42+
loadError.value = null;
43+
} catch (error) {
44+
console.error("Failed to load Mermaid component:", error);
45+
loadError.value = "图表加载失败,请刷新重试";
46+
}
3647
};
3748
3849
const renderWhenVisible = () => {
@@ -78,6 +89,10 @@ onBeforeUnmount(() => {
7889
font-size: 0.9rem;
7990
}
8091
92+
.mermaid-lazy-placeholder.is-error {
93+
color: var(--vp-c-danger);
94+
}
95+
8196
.mermaid-lazy-spinner {
8297
width: 1rem;
8398
height: 1rem;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"docs:build:clean": "rm -rf docs/.vuepress/.temp docs/.vuepress/.cache && pnpm docs:build",
2929
"docs:dev": "vuepress dev docs",
3030
"docs:clean-dev": "vuepress dev docs --clean-cache",
31+
"docsearch:index": "node scripts/docsearch-index.mjs",
3132
"lint": "pnpm lint:prettier && pnpm lint:md",
3233
"lint:md": "markdownlint-cli2 '**/*.md'",
3334
"lint:prettier": "prettier --check --write .",

0 commit comments

Comments
 (0)