perf(ip-info-card): reduce IP API load even more (#6263)

* refactor(ip-info-card): reduce retry, use succeed as lastFetchTs

* refactor(ip-info-card): stop countdown during revalidation

* perf(ip-info-card): avoid aggressive schedule revalidation

* perf(ip-info-card): try stop interval on window minimized

* perf(ip-info-card): only mutate after card scroll into view once

* perf(ip-info-card): interval only when card has been visible

* chore: add more debug information

* Update src/components/home/ip-info-card.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* fix: reset countdown state after mutate finishes

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Sukka
2026-02-06 09:09:36 +00:00
committed by GitHub
co-authored by Copilot
parent 781313e8f0
commit e9d63aee5e
3 changed files with 377 additions and 274 deletions
+8 -7
View File
@@ -186,10 +186,8 @@ function createPrng(seed: number): () => number {
export const getIpInfo = async (): Promise<
IpInfo & { lastFetchTs: number }
> => {
const lastFetchTs = Date.now();
// 配置参数
const maxRetries = 3;
const maxRetries = 2;
const serviceTimeout = 5000;
const shuffledServices = shuffleServices();
@@ -206,7 +204,7 @@ export const getIpInfo = async (): Promise<
try {
return await asyncRetry(
async (bail) => {
console.debug("Fetching IP information...");
console.debug("Fetching IP information:", service.url);
const response = await fetch(service.url, {
method: "GET",
@@ -226,15 +224,18 @@ export const getIpInfo = async (): Promise<
if (data && data.ip) {
debugLog(`IP检测成功,使用服务: ${service.url}`);
return Object.assign(service.mapping(data), { lastFetchTs });
return Object.assign(service.mapping(data), {
// use last fetch success timestamp
lastFetchTs: Date.now(),
});
} else {
throw new Error(`无效的响应格式 from ${service.url}`);
}
},
{
retries: maxRetries,
minTimeout: 500,
maxTimeout: 2000,
minTimeout: 1000,
maxTimeout: 4000,
randomize: true,
},
);