feat(web): report page views via sendBeacon and show stats on system page

This commit is contained in:
kairee committed 2026-11-01 15:36:42 +08:00
1 parent 3c16caac1a
commit b215f8eb09
4 files changed
+154 -3

No files matched your search

+24
View File
@@ -57,3 +57,27 @@ export async function session() {
const payload = await response.json();
return payload?.data ?? payload;
}
export async function stats(params) {
return api("/api/stats", params);
}
export function visit(path) {
// 访问统计埋点:优先 sendBeacon(不阻塞导航),失败时用 keepalive fetch 兜底。
const body = JSON.stringify({ path });
try {
if (typeof navigator !== "undefined" && typeof navigator.sendBeacon === "function") {
const blob = new Blob([body], { type: "application/json" });
if (navigator.sendBeacon("/api/visit", blob)) return;
}
} catch {
// 忽略,走 fetch 兜底
}
fetch("/api/visit", {
method: "POST",
headers: { "Content-Type": "application/json" },
body,
credentials: "same-origin",
keepalive: true,
}).catch(() => {});
}