Skip to content

fix(leaderboard): fallback 非数组覆盖空 (Copilot CR PR #326)#328

Merged
longsizhuo merged 1 commit intomainfrom
fix/leaderboard-fallback-non-array
Apr 27, 2026
Merged

fix(leaderboard): fallback 非数组覆盖空 (Copilot CR PR #326)#328
longsizhuo merged 1 commit intomainfrom
fix/leaderboard-fallback-non-array

Conversation

@longsizhuo
Copy link
Copy Markdown
Member

CR 反馈

PR #326 Copilot 指出 `scripts/generate-leaderboard.mjs:178` 的 fallback 只要 JSON.parse 成功就 `preservedExisting=true`,包括对象/null/字面量等非数组类型也会被"维持原状"。但下游 `Hero.tsx` / rank 页等多处 `import leaderboardData from "@/generated/site-leaderboard.json"` 直接 `.filter/.map`,一旦内容不是数组,整个 Next build 因 `filter is not a function` 直接挂掉。

改动

四档 fallback:

既有 JSON 内容 行为
非空数组 保留(warn 多少条)
空数组 保留 空数组(语义合法,下游 .filter 不挂)
JSON 损坏 兜底覆盖 `[]`
非数组(对象 / null / 字面量等) 兜底覆盖 `[]` ← 关键修复
文件不存在 兜底覆盖 `[]`

本地验证

  • 非数组对象 → 兜底为 `[]` ✅
  • 空数组 `[]` → 保留 ✅
  • 非空 21 条数组 → 保留 ✅

Copilot CR (PR #326) 反馈
之前 fallback 只要 JSON.parse 成功就 preserveExisting=true,包括对象、null
等非数组类型也会被"维持原状"。但下游有多处 `import leaderboardData from
"@/generated/site-leaderboard.json"` 直接 .filter/.map(如 Hero 的 top3、
/rank 页),一旦内容不是数组,整个 Next build 就会因
"filter is not a function" 直接挂掉。

修法
四档 fallback:
  1. 非空数组         → 保留(warn 多少条)
  2. 空数组           → 保留空数组(语义合法,下游 .filter 不挂)
  3. JSON 损坏 / 非数组 → 兜底覆盖为 [](避免下游 import 后 type error)
  4. 文件不存在        → 兜底覆盖为 []

效果
- 任何已存在的合法数组都不被无故覆盖
- 任何非数组数据都强制规范化为 [],下游 .filter/.map 永远 safe
Copilot AI review requested due to automatic review settings April 27, 2026 16:51
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 27, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
involutionhell-github-io Building Building Preview, Comment Apr 27, 2026 4:51pm
website-preview Building Building Preview, Comment Apr 27, 2026 4:51pm

@longsizhuo longsizhuo merged commit b20ea9a into main Apr 27, 2026
6 of 8 checks passed
@longsizhuo longsizhuo deleted the fix/leaderboard-fallback-non-array branch April 27, 2026 16:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

调整 leaderboard 生成脚本在后端不可用时的降级策略,避免生成/保留非数组 JSON 导致下游 .filter/.map 在 Next build 期间直接崩溃。

Changes:

  • 将“保留既有 JSON”的条件收紧为“既有内容为数组”(非数组类型视为无效并用 [] 覆盖)。
  • 明确保留空数组 [](语义合法且下游不会因类型错误崩溃)。
  • 更新降级分支的告警日志与注释说明,区分数组/非数组/JSON 损坏/文件不存在等场景。

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +187 to 190
// 非数组(对象、字面量、null 等)→ 不 preserve,走下方兜底覆盖空数组
console.warn(
`[generate-leaderboard] 后端不可用,且 ${OUTPUT} 已有内容非有效非空数组,维持原状。`,
`[generate-leaderboard] ${OUTPUT} 已存在但内容不是数组(typeof=${typeof parsed}),按无效数据处理,兜底覆盖为 []。`,
);
Copy link

Copilot AI Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

日志里用 typeof parsed 来提示“非数组”类型时,遇到 null 会显示为 object(JS 里 typeof null === "object"),容易误导排查。建议在日志中把 null 单独显示出来(例如显示为 null),或补充更明确的类型描述。

Copilot uses AI. Check for mistakes.
longsizhuo added a commit that referenced this pull request Apr 27, 2026
Copilot CR (PR #328)
JS 历史包袱:typeof null === "object"。之前 fallback 兜底分支日志写
"kind=object",遇到既有内容是 null 时排查者会误以为是个普通对象,绕半天。
单独识别 null,日志输出 kind=null。

实测三种情况日志输出:
- null    → kind=null
- {...}   → kind=object
- "str"   → kind=string

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants