Skip to content

Commit 79d2cef

Browse files
committed
chore(rank): CR - RankTabs query 校验 + PageViewTracker storage try/catch
Copilot CR #276: - RankTabs: useSearchParams 返回 string|null,之前直接 as Tab/Window 没校验, ?tab=foo 或 ?window=1d 会让下游组件拿到非法值导致空白渲染。 加白名单 + type guard,非法时 fallback 到 initial* - DocsPageViewTracker: sessionStorage / localStorage 包 try/catch Safari 隐私模式或存储禁用时不崩,降级到继续上报但不去重
1 parent f3852d1 commit 79d2cef

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

app/components/DocsPageViewTracker.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,26 @@ export function DocsPageViewTracker() {
2020
useEffect(() => {
2121
if (!pathname) return;
2222

23-
// 同会话同 path 已上报则跳过,避免刷新/快速切换重复计数
23+
// 同会话同 path 已上报则跳过,避免刷新/快速切换重复计数。
24+
// sessionStorage / localStorage 在 Safari 隐私模式、存储禁用、配额超限时会抛错,
25+
// 埋点组件要绝对静默,全部包 try/catch 后降级到"继续上报但不去重"即可。
2426
const key = `pv_reported:${pathname}`;
25-
if (sessionStorage.getItem(key)) return;
26-
27-
sessionStorage.setItem(key, "1");
27+
try {
28+
if (sessionStorage.getItem(key)) return;
29+
sessionStorage.setItem(key, "1");
30+
} catch {
31+
// storage 不可用,跳过去重继续上报
32+
}
2833

2934
// 如果用户登录了,带上 Sa-Token 让后端能把事件关联到 userId;匿名用户后端会写入 userId=null
30-
const token =
31-
typeof window !== "undefined" ? localStorage.getItem("satoken") : null;
35+
let token: string | null = null;
36+
if (typeof window !== "undefined") {
37+
try {
38+
token = localStorage.getItem("satoken");
39+
} catch {
40+
token = null;
41+
}
42+
}
3243
const headers: Record<string, string> = {
3344
"Content-Type": "application/json",
3445
};

0 commit comments

Comments
 (0)