Skip to content

Commit 4fd9d75

Browse files
committed
fix: add skipCache parameter to control caching behavior in overview API (close #29 )
1 parent 2655514 commit 4fd9d75

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

app/api/overview/route.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,14 @@ export async function GET(request: Request) {
6969

7070
const page = pageParam ? Number.parseInt(pageParam, 10) : undefined;
7171
const pageSize = pageSizeParam ? Number.parseInt(pageSizeParam, 10) : undefined;
72+
const skipCacheParam = searchParams.get("skipCache");
73+
const skipCache = skipCacheParam === "1" || skipCacheParam === "true";
7274
const cacheKey = makeCacheKey({ days, model, route, page, pageSize, start, end });
73-
const cached = getCached(cacheKey);
74-
if (cached) {
75-
return NextResponse.json(cached, { status: 200 });
75+
if (!skipCache) {
76+
const cached = getCached(cacheKey);
77+
if (cached) {
78+
return NextResponse.json(cached, { status: 200 });
79+
}
7680
}
7781

7882
const { overview, empty, days: appliedDays, meta, filters } = await getOverview(days, {

app/page.tsx

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export default function DashboardPage() {
201201
const pieChartFullscreenContainerRef = useRef<HTMLDivElement | null>(null);
202202
const pieLegendClearTimerRef = useRef<number | null>(null);
203203
const syncingRef = useRef(false);
204+
const skipOverviewCacheRef = useRef(false);
204205
const [pendingDelete, setPendingDelete] = useState<string | null>(null);
205206
const [syncingPrices, setSyncingPrices] = useState(false);
206207
// const [pricesSyncStatus, setPricesSyncStatus] = useState<SyncStatus>({ type: 'idle' }); // 已禁用 toast 通知
@@ -532,7 +533,10 @@ export default function DashboardPage() {
532533
window.localStorage.setItem("lastSyncStatus", successMsg);
533534
}
534535
}
535-
if (triggerRefresh && inserted > 0) setRefreshTrigger((prev) => prev + 1);
536+
if (triggerRefresh && inserted > 0) {
537+
skipOverviewCacheRef.current = true;
538+
setRefreshTrigger((prev) => prev + 1);
539+
}
536540
}
537541
} catch (err) {
538542
// 判断是否为超时错误
@@ -634,7 +638,7 @@ export default function DashboardPage() {
634638

635639
const run = async () => {
636640
try {
637-
await doSync(true, false, 5000); // 首屏加载使用 5 秒超时
641+
await doSync(true, true, 5000); // 首屏加载使用 5 秒超时
638642
if (typeof window !== "undefined") {
639643
window.sessionStorage.setItem(autoSyncKey, "1");
640644
}
@@ -692,6 +696,11 @@ export default function DashboardPage() {
692696
params.set("page", String(page));
693697
params.set("pageSize", "500");
694698

699+
if (skipOverviewCacheRef.current) {
700+
params.set("skipCache", "1");
701+
skipOverviewCacheRef.current = false;
702+
}
703+
695704
const res = await fetch(`/api/overview?${params.toString()}`, { cache: "no-store", signal: controller.signal });
696705

697706
if (!res.ok) {

0 commit comments

Comments
 (0)