Skip to content

Open source CapCut alternative.

TypeScript

perf(site): the analytics rollup is warmed on shell mount and held for an hour

David Leca7e7e3e49b8
diff --git a/site/src/app/su/SuShell.tsx b/site/src/app/su/SuShell.tsx
index 7e95ad53..5ffa3f7c 100644
--- a/site/src/app/su/SuShell.tsx
+++ b/site/src/app/su/SuShell.tsx
@@ -5,11 +5,13 @@ import type { ReactNode } from "react";
import { NoSessionReplay } from "@/app/_components/NoSessionReplay";
import { SuHeader } from "@/app/su/SuHeader";
import { SuSidebar } from "@/app/su/SuSidebar";
+import { useWarmAnalyticsRollup } from "@/queries/analytics";
// The section's two-pane shell. It mounts only for a super user: the proxy
// (src/proxy.ts) has already checked the session and the role before the
// route runs.
export function SuShell({ children }: { children: ReactNode }) {
+ useWarmAnalyticsRollup();
// The section paints the white product surface. `app-surface` is what
// repoints the --background token for the whole document (see globals.css),
// so it belongs on the outermost element here.
diff --git a/site/src/queries/analytics.ts b/site/src/queries/analytics.ts
index 54cd908b..39618f0c 100644
--- a/site/src/queries/analytics.ts
+++ b/site/src/queries/analytics.ts
@@ -1,6 +1,7 @@
"use client";
-import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+import { queryOptions, useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+import { useEffect } from "react";
import type { AnalyticsRollup } from "@/lib/analytics/schema";
import { apiFetch } from "@/queries/apiClient";
@@ -8,13 +9,26 @@ import type { AsyncJobStatus } from "@/queries/jobs";
export const analyticsRollupQueryKey = ["analytics", "rollup"] as const;
-// Super-user only: the nightly job's consolidated rollup. Stale until the
-// next run by design.
+// Super-user only: the nightly job's consolidated rollup. It changes once a
+// night, and a manual run invalidates it, so a copy in memory stays good for
+// the session.
+const analyticsRollupQuery = queryOptions({
+ queryFn: () => apiFetch<AnalyticsRollup>("/api/analytics/rollup"),
+ queryKey: analyticsRollupQueryKey,
+ staleTime: 60 * 60_000,
+});
+
export function useAnalyticsRollup() {
- return useQuery({
- queryFn: () => apiFetch<AnalyticsRollup>("/api/analytics/rollup"),
- queryKey: analyticsRollupQueryKey,
- });
+ return useQuery(analyticsRollupQuery);
+}
+
+// Pulls the rollup as soon as the super-user shell mounts, so the Product tab
+// paints from memory when it opens.
+export function useWarmAnalyticsRollup() {
+ const queryClient = useQueryClient();
+ useEffect(() => {
+ void queryClient.prefetchQuery(analyticsRollupQuery);
+ }, [queryClient]);
}
// Super-user only: run the analytics-daily job now and follow it to