Skip to content

Open source alternative to Semrush and Ahrefs

Refresh rank tracking charts and history after checks (EVE-259) (#661)

Carter Smith2e2104208e6c
diff --git a/src/client/features/rank-tracking/useRankRunPolling.ts b/src/client/features/rank-tracking/useRankRunPolling.ts
index acdf0aa2..8672fbfc 100644
--- a/src/client/features/rank-tracking/useRankRunPolling.ts
+++ b/src/client/features/rank-tracking/useRankRunPolling.ts
@@ -4,34 +4,42 @@ import { getLatestRankRun } from "@/serverFunctions/rank-tracking";
/**
* Polls the latest rank check run for a config, auto-refreshing results
- * when a run transitions from "running" to "completed".
+ * and history when it observes a completed or failed run.
*/
export function useRankRunPolling(projectId: string, configId: string) {
const queryClient = useQueryClient();
- const prevStatusRef = useRef<string | undefined>(undefined);
+ const lastTerminalRunRef = useRef<string | undefined>(undefined);
const { data: latestRun } = useQuery({
queryKey: ["rankTrackingLatestRun", projectId, configId],
queryFn: () => getLatestRankRun({ data: { projectId, configId } }),
refetchInterval: (query) => {
const run = query.state.data;
- const prev = prevStatusRef.current;
- prevStatusRef.current = run?.status;
-
- // When a run transitions to a terminal state, invalidate results
+ // A run can start and finish between polls, without an active observation.
const isTerminal =
run?.status === "completed" || run?.status === "failed";
- const wasActive = prev === "running" || prev === "pending";
- if (wasActive && isTerminal) {
+ const terminalRunKey = `${projectId}:${configId}:${run?.id}:${run?.status}`;
+ if (isTerminal && lastTerminalRunRef.current !== terminalRunKey) {
+ lastTerminalRunRef.current = terminalRunKey;
void queryClient.invalidateQueries({
queryKey: ["rankTrackingResults", projectId, configId],
});
+ void queryClient.invalidateQueries({
+ queryKey: ["rankConfigTrend", projectId, configId],
+ });
+ void queryClient.invalidateQueries({
+ queryKey: ["rankPositionMatrix", projectId, configId],
+ });
+ void queryClient.invalidateQueries({
+ queryKey: ["rankKeywordHistory", projectId, configId],
+ });
}
// Keep polling active runs, including stale ones (they'll be cleaned up
// by the cron handler and we want to show the transition).
if (run?.status === "pending" || run?.status === "running") return 3000;
- return false;
+ // Discover scheduled checks and checks started from another client.
+ return 30_000;
},
});