Skip to content

Commit be33c5c

Browse files
committed
优化 hits 获取方式
1 parent a1b0c5c commit be33c5c

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/components/react/HitsInfo.tsx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,15 @@ const HitsInfo: React.FC<Props> = ({ tag, label, today: todayLabel, total: total
1818
const hitsUrl = new URL("https://hits.zkitefly.eu.org");
1919
hitsUrl.searchParams.set("tag", tag);
2020

21-
fetch(hitsUrl)
21+
fetch(hitsUrl, { method: "HEAD" })
2222
.then(async (response) => {
2323
if (response.status !== 200) return;
24-
25-
const svg = await response.text();
26-
const parser = new DOMParser();
27-
const doc = parser.parseFromString(svg, "image/svg+xml");
28-
const textNode = doc.querySelector("text");
29-
30-
if (!textNode || !textNode.textContent) return;
31-
32-
const parts = textNode.textContent.split(" / ");
33-
if (parts.length !== 2) return;
34-
35-
const [total, today] = parts;
36-
setData({ total, today });
24+
const { headers } = response;
25+
const total = headers.get("X-Total-Hits");
26+
const today = headers.get("X-Today-Hits");
27+
if (total !== null && today !== null) {
28+
setData({ total, today });
29+
}
3730
})
3831
.catch(() => {});
3932
}, []);

0 commit comments

Comments
 (0)