Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
perf(insights): load the page in one server action, once (#957)
Gökhan Geyikc3485c3c4ef6
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsxindex 24d60006a..86278a72c 100644--- a/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx+++ b/web/src/app/[locale]/(dashboard)/dashboard/insights/page.tsx@@ -43,7 +43,6 @@ import { FormulaDialog } from './_formula-dialog';import { useBrandStore } from '@/stores/use-brand-store';import {getInsightsData,- getVisibilityRateTrend,getTrackingWindow,type TrackingWindow,type VisibilityRateTrendData,@@ -989,35 +988,20 @@ export default function InsightsPage() {const filtersRef = useRef(filters);filtersRef.current = filters;+ // Keyed on the id, not the brand object: the store hands out a new object+ // for the same brand, and a new loadData re-runs the load effect below.+ const brandId = brand?.id;const loadData = useCallback(async (overrideFilters?: InsightsFilters, { silent = false } = {}) => {- if (!brand) return;+ if (!brandId) return;if (!silent) setIsLoading(true);try {const f = overrideFilters ?? filtersRef.current;- // eslint-disable-next-line prefer-const -- dateFrom/dateTo are reassigned by the 24h anchor below- let { dateFrom, dateTo, days } = getDateRange(f.datePreset, {+ const { dateFrom, dateTo, days } = getDateRange(f.datePreset, {from: f.dateFrom,to: f.dateTo,});- // Stable 24h window (00044): anchor to the last COMPLETED tracking- // run so the morning refresh can't flash "no results" or shrink the- // counts while today's run streams in. Brands with no completed run- // yet (fresh onboarding) keep the live rolling window — nothing of- // theirs is old enough to age out, so it can't regress.- if (f.datePreset === '24h') {- const win = await getTrackingWindow(brand.id).catch(() => null);- if (win) {- cronRunRef.current = win.activeRun;- setCronRun(win.activeRun);- if (win.anchored) {- dateFrom = win.anchored.dateFrom;- dateTo = win.anchored.dateTo;- }- }- }-const filterOpts = {model: f.model || undefined,region: f.region || undefined,@@ -1032,20 +1016,25 @@ export default function InsightsPage() {const hasFilters = Boolean(f.datePreset !== 'all' || f.model || f.region || f.topic);- // One consolidated server action (#313): summary + competitor + SoV- // run in a real server-side Promise.all (one round trip instead of- // five serialized POSTs), and "has any data" comes from a cheap- // count instead of an unbounded full-table scan.- // The trend chart follows the page filters exactly — same window as- // every other number on the page, so the headline rate never- // contradicts the KPI header.- const [insights, trend] = await Promise.all([- getInsightsData(brand.id, {- ...filterOpts,- checkUnfiltered: hasFilters,- }),- getVisibilityRateTrend(brand.id, filterOpts).catch(() => null),- ]);+ // One consolidated server action (#313). Next.js queues server actions+ // one after another, so everything the first paint needs — summary,+ // competitors, SoV, KPIs and the trend chart — runs in one+ // server-side Promise.all instead of as separate requests, and "has+ // any data" comes from a cheap count.+ // The 24h preset is anchored to the last completed tracking run+ // (00044) so the morning refresh can't flash "no results" while+ // today's run streams in; the action resolves that window itself.+ // The trend chart follows the same window as every other number.+ const insights = await getInsightsData(brandId, {+ ...filterOpts,+ checkUnfiltered: hasFilters,+ anchor24h: f.datePreset === '24h',+ });+ if (insights.trackingWindow) {+ cronRunRef.current = insights.trackingWindow.activeRun;+ setCronRun(insights.trackingWindow.activeRun);+ }+ const trend = insights.trend;setRateTrend(trend && trend.points.length > 0 ? trend : null);setSummary(insights.summary);setTrackedPrompts(insights.trackedPrompts);@@ -1107,7 +1096,7 @@ export default function InsightsPage() {setIsLoading(false);}},- [brand],+ [brandId],);useEffect(() => {diff --git a/web/src/lib/actions/tracking.ts b/web/src/lib/actions/tracking.tsindex 7f045ec56..fc66330b8 100644--- a/web/src/lib/actions/tracking.ts+++ b/web/src/lib/actions/tracking.ts@@ -712,6 +712,10 @@ export interface InsightsData {filterOptions: InsightsFilterOptions;recommendations: InsightsRecommendations;hasAnyData: boolean;+ /** The visibility-rate chart for the same window; null when its read failed. */+ trend: VisibilityRateTrendData | null;+ /** The run ledger, when the 24h window was anchored to it (`anchor24h`). */+ trackingWindow: TrackingWindow | null;}export interface TrackingWindow {@@ -804,14 +808,23 @@ export async function getInsightsData(days?: DayWindow;/** When the current view is filtered, check unfiltered data existence too. */checkUnfiltered?: boolean;+ /**+ * The 24h preset: anchor dateFrom/dateTo to the last completed tracking+ * run (see getTrackingWindow). Resolved here rather than by a separate+ * client call, because each server action is its own queued request.+ */+ anchor24h?: boolean;},): Promise<InsightsData> {+ const trackingWindow = opts.anchor24h ? await getTrackingWindow(brandId).catch(() => null) : null;+ const anchored = trackingWindow?.anchored;+const filterOpts = {model: opts.model,region: opts.region,topicId: opts.topicId,- dateFrom: opts.dateFrom,- dateTo: opts.dateTo,+ dateFrom: anchored?.dateFrom ?? opts.dateFrom,+ dateTo: anchored?.dateTo ?? opts.dateTo,days: opts.days,};@@ -830,6 +843,7 @@ export async function getInsightsData(filterOptions,recommendations,unfilteredHasData,+ trend,] = await Promise.all([getInsightsSummary(brandId, filterOpts),getCompetitorComparison(brandId, filterOpts).catch((err) => {@@ -850,6 +864,11 @@ export async function getInsightsData(getInsightsFilterOptions(brandId),getInsightsRecommendations(brandId),opts.checkUnfiltered ? brandHasResults(brandId) : Promise.resolve(null),+ // The chart follows the same window as every other number on the page.+ getVisibilityRateTrend(brandId, filterOpts).catch((err) => {+ console.error('[insights] visibility trend failed', err);+ return null;+ }),]);// insights_aggregates counts the same filtered set the summary shows, so it@@ -865,6 +884,8 @@ export async function getInsightsData(filterOptions,recommendations,hasAnyData,+ trend,+ trackingWindow,};}