F1-4: schema 页全量接入——错误边界、健康度扫描、抽样渲染实测

渲染链路加固:
- PageRenderPage 加 AmisErrorBoundary class component + SchemaErrorCard,覆盖
  schema 拉取失败/JSON 异常/amis 渲染抛错三种路径,不白屏不崩 Tab
- schema 加载成功后调用 recordRecentPage 写入最近打开
- src/api/schema.ts mock 模式下未配置 templateId 自动 fallback 后端接口

全量健康度扫描 (scripts/schema-scan.mjs):
- 登录→psql 拉取 1489 个 template_id→并发≤8 扫描→产出报告
- 结果: 1489/1489 HTTP 200,JSON 可解析率 100%,顶层 type 全为 page,零失败

抽样渲染实测 (e2e/schema-samples.spec.ts):
- 9 种类型 26 个样本,Node 侧预登录注入真实 JWT,断言 amis-host 挂载且无错误卡片
- 结果: 26/26 全部通过,截图存 e2e/artifacts/

配置更新:
- playwright.config.ts 后端反代改为 8093 (dev profile,含所有端点)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
2026-06-11 20:32:53 +08:00
parent effc21d929
commit 0d8593e022
33 changed files with 597 additions and 18 deletions

View File

@@ -1,9 +1,74 @@
// 页面渲染路由 /page/:templateId —— 取转换后 schema 交 amis 渲染
import { useEffect, useState } from 'react';
// F1-4:加错误边界(schema 拉取失败/JSON 异常/amis 渲染抛错均显示页内错误卡片)
import { Component, useEffect, useState } from 'react';
import type { ReactNode } from 'react';
import { useParams } from 'react-router-dom';
import { fetchSchema } from '../api/schema';
import { AmisRenderer } from '../components/AmisRenderer';
import { recordRecentPage } from '../utils/recentPages';
// ---- amis 渲染层错误边界(class component,捕获子树抛错)----
interface ErrorBoundaryState {
caught: boolean;
message: string;
}
class AmisErrorBoundary extends Component<
{ templateId: string; children: ReactNode },
ErrorBoundaryState
> {
constructor(props: { templateId: string; children: ReactNode }) {
super(props);
this.state = { caught: false, message: '' };
}
static getDerivedStateFromError(error: unknown): ErrorBoundaryState {
const msg = error instanceof Error ? error.message : String(error);
return { caught: true, message: msg };
}
override componentDidCatch() {
// 静默记录,不向上抛
}
override render() {
if (this.state.caught) {
return (
<SchemaErrorCard
templateId={this.props.templateId}
summary={'amis 渲染异常:' + this.state.message}
/>
);
}
return this.props.children;
}
}
// ---- 统一错误卡片 ----
function SchemaErrorCard({ templateId, summary }: { templateId: string; summary: string }) {
return (
<div
data-testid="schema-error-card"
style={{
margin: 24,
padding: '20px 24px',
border: '1px solid #f5c6cb',
borderRadius: 6,
background: '#fff5f5',
color: '#721c24',
maxWidth: 640,
}}
>
<div style={{ fontWeight: 600, marginBottom: 8, fontSize: 15 }}>页面加载失败</div>
<div style={{ marginBottom: 6, fontSize: 13, color: '#555' }}>
模板 ID:<code style={{ background: '#f1f1f1', padding: '1px 4px', borderRadius: 3 }}>{templateId}</code>
</div>
<div style={{ fontSize: 13, color: '#a94442', wordBreak: 'break-all' }}>{summary}</div>
</div>
);
}
// ---- 主组件 ----
export function PageRenderPage() {
const { templateId = '' } = useParams();
const [schema, setSchema] = useState<Record<string, unknown> | null>(null);
@@ -15,10 +80,19 @@ export function PageRenderPage() {
setError('');
fetchSchema(templateId)
.then((s) => {
if (alive) setSchema(s);
if (!alive) return;
// 校验顶层结构:必须是对象,否则视为 JSON 异常
if (typeof s !== 'object' || s === null || Array.isArray(s)) {
setError('schema 格式异常:顶层不是 JSON 对象');
return;
}
setSchema(s);
// 渲染成功后写入最近打开(title 取 schema.title 或 templateId)
const title = (s['title'] as string) || templateId;
recordRecentPage({ templateId, name: title, path: title });
})
.catch((e: Error) => {
if (alive) setError(e.message);
if (alive) setError(e.message ?? '未知错误');
});
return () => {
alive = false;
@@ -26,14 +100,20 @@ export function PageRenderPage() {
}, [templateId]);
if (error) {
return <div style={{ padding: 24, color: '#d9534f' }}>加载页面失败:{error}</div>;
return <SchemaErrorCard templateId={templateId} summary={error} />;
}
if (!schema) {
return <div style={{ padding: 24, color: '#888' }}>加载中…</div>;
return (
<div style={{ padding: 24, color: '#888' }} data-testid="schema-loading">
加载中…
</div>
);
}
return (
<div style={{ height: '100%' }} data-testid="amis-host">
<AmisRenderer schema={schema} />
<AmisErrorBoundary templateId={templateId}>
<AmisRenderer schema={schema} />
</AmisErrorBoundary>
</div>
);
}