Skip to content

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.

perf(audit): load the Site Audit hub in two requests instead of seven in a row (#1202)

Cihan930a0bd9b4ff
diff --git a/server/src/routes/audits-opportunities.test.js b/server/src/routes/audits-opportunities.test.js
index 218296ccb..8046f8a48 100644
--- a/server/src/routes/audits-opportunities.test.js
+++ b/server/src/routes/audits-opportunities.test.js
@@ -79,6 +79,8 @@ async function startApp() {
}
return { data: tables.competitorPrompts ?? [], error: null };
}
+ // A test may make the AI visits step take a while.
+ tables.onVisits?.();
return {
data: {
analytics: [{ page: args.p_pages[0], platform: 'chatgpt', cur_sessions: 7 }],
@@ -200,6 +202,37 @@ describe('GET /api/audits/opportunities', () => {
expect(args.p_pages).toEqual(['https://acme.com/pricing']);
});
+ it('says which step a slow read spent its time on, and nothing for a quick one', async () => {
+ const { default: logger } = await import('../lib/logger.js');
+ await fetch(`${baseUrl}/opportunities?brandId=${brandId}`);
+ expect(logger.warn).not.toHaveBeenCalled();
+
+ // The clock moves 2.5 s while the AI visits are read.
+ const realNow = Date.now.bind(Date);
+ let offset = 0;
+ const clock = vi.spyOn(Date, 'now').mockImplementation(() => realNow() + offset);
+ tables.onVisits = () => {
+ offset += 2500;
+ };
+ try {
+ await fetch(`${baseUrl}/opportunities?brandId=${brandId}`);
+ } finally {
+ clock.mockRestore();
+ }
+ const call = logger.warn.mock.calls.find(
+ ([, msg]) => msg === '[audit] slow opportunities inbox read',
+ );
+ expect(call[0]).toMatchObject({ brandId, rows: 1 });
+ expect(call[0].ms.aiVisits).toBeGreaterThanOrEqual(2500);
+ expect(call[0].ms.total).toBeGreaterThanOrEqual(call[0].ms.aiVisits);
+ expect(call[0].ms).toEqual({
+ reads: expect.any(Number),
+ competitorsAndTemplates: expect.any(Number),
+ aiVisits: expect.any(Number),
+ total: expect.any(Number),
+ });
+ });
+
it("leaves out pages under the brand's exclude patterns (SA-006)", async () => {
tables.site_audit_brand_settings = [{ brand_id: brandId, exclude_patterns: ['/pri*'] }];
const body = await (await fetch(`${baseUrl}/opportunities?brandId=${brandId}`)).json();
diff --git a/server/src/routes/audits.js b/server/src/routes/audits.js
index c063924bd..90be5594c 100644
--- a/server/src/routes/audits.js
+++ b/server/src/routes/audits.js
@@ -733,7 +733,11 @@ async function readExcludePatterns(brandId) {
* pages to put back. The route shows it; auto-audit runs take their pages
* from it (SA-006).
*/
+/** An inbox read slower than this logs how long each of its steps took. */
+const SLOW_INBOX_MS = 2000;
+
export async function readOpportunityInbox(brandId) {
+ const startedAt = Date.now();
const [primary, gaPages, sitePages, audits, prefs, excludePatterns] = await Promise.all([
primaryDomainOf(brandId),
// Up to 30% of GA's 5,000 pages a day: past PostgREST's 1000-row page.
@@ -759,6 +763,7 @@ export async function readOpportunityInbox(brandId) {
readExcludePatterns(brandId),
]);
for (const read of [gaPages, sitePages, audits, prefs]) if (read.error) throw read.error;
+ const readsDoneAt = Date.now();
const primaryHost = normStoredDomain(primary);
const auditPages = buildAuditPages(audits.data);
@@ -766,6 +771,7 @@ export async function readOpportunityInbox(brandId) {
competitorOpportunityPages(brandId, sitePages.data ?? []),
readTemplateGroups({ primaryHost, sitePages: sitePages.data ?? [], auditPages }),
]);
+ const sourcesDoneAt = Date.now();
const { rows, excluded } = buildOpportunities({
primaryHost,
gaPages: gaPages.data ?? [],
@@ -787,6 +793,25 @@ export async function readOpportunityInbox(brandId) {
const visitsOf = (urls) =>
visits === null ? null : urls.reduce((sum, url) => sum + (visits.get(url) ?? 0), 0);
+ // The hub waits on this read (13 s on production, 2026-10-19): say which
+ // step is slow, so it can be made faster.
+ const doneAt = Date.now();
+ if (doneAt - startedAt >= SLOW_INBOX_MS) {
+ logger.warn(
+ {
+ brandId,
+ rows: rows.length + templateRows.length,
+ ms: {
+ reads: readsDoneAt - startedAt,
+ competitorsAndTemplates: sourcesDoneAt - readsDoneAt,
+ aiVisits: doneAt - sourcesDoneAt,
+ total: doneAt - startedAt,
+ },
+ },
+ '[audit] slow opportunities inbox read',
+ );
+ }
+
return {
// Scored and ordered once each page has its AI visits (SA-005); a page
// whose template shares its gaps is easier to fix (SA-007).
diff --git a/web/messages/en.json b/web/messages/en.json
index 1ec775acb..84c26e5d6 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -2159,7 +2159,8 @@
"label": "Site Audit lists",
"opportunities": "Opportunities",
"pages": "Pages",
- "noPages": "No pages audited yet. Audit an opportunity or enter a URL above."
+ "noPages": "No pages audited yet. Audit an opportunity or enter a URL above.",
+ "loading": "Loading"
},
"opportunities": {
"priority": "Priority",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/audit/page.tsx b/web/src/app/[locale]/(dashboard)/dashboard/audit/page.tsx
index baecf32d3..73690d223 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/audit/page.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/audit/page.tsx
@@ -28,12 +28,9 @@ import {
runAudit,
runAuditsBulk,
getAuditPages,
- getAuditOverview,
- getAuditOpportunities,
+ getAuditHub,
+ getAuditHubExtras,
setOpportunityPrefs,
- getAuditQuota,
- getAuditTrend,
- getLatestAutoRun,
getAuditSettings,
getAuditTemplates,
saveAuditSettings,
@@ -64,36 +61,6 @@ import { cn } from '@/lib/utils';
type RangePreset = '7d' | '30d' | '90d' | 'all';
const RANGE_DAYS: Record<RangePreset, number | null> = { '7d': 7, '30d': 30, '90d': 90, all: null };
-/** The KPI cards' numbers; the cards are left out when they can't be read. */
-async function readOverview(brandId: string): Promise<AuditOverview | null> {
- try {
- return await getAuditOverview(brandId);
- } catch (err) {
- console.error('Failed to load the Site Audit overview:', err);
- return null;
- }
-}
-
-/** The Opportunities inbox; its tab is left out when it can't be read. */
-async function readOpportunities(brandId: string): Promise<AuditOpportunities | null> {
- try {
- return await getAuditOpportunities(brandId);
- } catch (err) {
- console.error('Failed to load the Site Audit opportunities:', err);
- return null;
- }
-}
-
-/** The latest auto-audit run on the brand's pages; no notice when it can't be read. */
-async function readLatestRun(brandId: string): Promise<AutoAuditRun | null> {
- try {
- return await getLatestAutoRun(brandId);
- } catch (err) {
- console.error('Failed to load the latest auto-audit run:', err);
- return null;
- }
-}
-
/** Pages one bulk request may audit (the server's BULK_AUDIT_MAX). */
const BULK_REQUEST_MAX = 50;
@@ -145,6 +112,8 @@ export default function SiteAuditPage() {
const [range, setRange] = useState<RangePreset>('30d');
const [selected, setSelected] = useState<Set<string>>(() => new Set());
const [opps, setOpps] = useState<AuditOpportunities | null>(null);
+ // The inbox loads after the rest of the hub (getAuditHubExtras).
+ const [oppsLoading, setOppsLoading] = useState(true);
const [oppSelected, setOppSelected] = useState<Set<string>>(() => new Set());
// Opportunities when it lists pages, else Pages; chosen once per brand.
const [tab, setTab] = useState<ListTab | null>(null);
@@ -168,41 +137,43 @@ export default function SiteAuditPage() {
listsRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' });
};
- // Load the audited pages + the monthly quota + the primary-domain trend.
+ // Load the hub in two requests: the audited pages, the quota, the trend
+ // and the cards first, so the page shows; then the Opportunities inbox and
+ // the latest auto-audit run, which take longer, fill in.
useEffect(() => {
if (!activeBrandId) return;
let cancelled = false;
setLoading(true);
+ setOppsLoading(true);
setError(null);
// A selection, and the tab, belong to the brand they were made on.
setSelected(new Set());
setOppSelected(new Set());
setTab(null);
+ setOpps(null);
setLatestRun(null);
(async () => {
try {
- const [data, q, tr, ov, op, run] = await Promise.all([
- getAuditPages(activeBrandId),
- getAuditQuota(),
- getAuditTrend(activeBrandId),
- readOverview(activeBrandId),
- readOpportunities(activeBrandId),
- readLatestRun(activeBrandId),
- ]);
- if (!cancelled) {
- setPages(data.pages);
- setPagesTotal(data.total);
- setPrimaryHost(data.primaryHost);
- setQuota(q);
- setTrend(tr);
- setOverview(ov);
- setOpps(op);
- setLatestRun(run);
- setTab(
- (current) =>
- current ?? ((op?.opportunities.length ?? 0) > 0 ? 'opportunities' : 'pages'),
- );
- }
+ const hub = await getAuditHub(activeBrandId);
+ if (cancelled) return;
+ setPages(hub.pages.pages);
+ setPagesTotal(hub.pages.total);
+ setPrimaryHost(hub.pages.primaryHost);
+ setQuota(hub.quota);
+ setTrend(hub.trend);
+ setOverview(hub.overview);
+ setLoading(false);
+
+ const extras = await getAuditHubExtras(activeBrandId);
+ if (cancelled) return;
+ setOpps(extras.opportunities);
+ setLatestRun(extras.latestRun);
+ // The inbox is the first tab when it has pages, unless one was picked meanwhile.
+ setTab(
+ (current) =>
+ current ??
+ ((extras.opportunities?.opportunities.length ?? 0) > 0 ? 'opportunities' : 'pages'),
+ );
} catch (err) {
console.error('Failed to load audit hub data:', err);
if (!cancelled) {
@@ -213,6 +184,7 @@ export default function SiteAuditPage() {
} finally {
if (!cancelled) {
setLoading(false);
+ setOppsLoading(false);
}
}
})();
@@ -332,28 +304,24 @@ export default function SiteAuditPage() {
shownRef.current = pages.length;
}, [pages.length]);
const reloadInPlace = async (brandId: string) => {
- const [data, q, tr, ov, op, run] = await Promise.all([
- getAuditPages(brandId, 0, Math.min(Math.max(shownRef.current, 50), 100)),
- getAuditQuota(),
- getAuditTrend(brandId),
- readOverview(brandId),
- readOpportunities(brandId),
- readLatestRun(brandId),
- ]);
+ const hub = await getAuditHub(brandId, Math.min(Math.max(shownRef.current, 50), 100));
+ const data = hub.pages;
setPages(data.pages);
setPagesTotal(data.total);
setPrimaryHost(data.primaryHost);
- setQuota(q);
- setTrend(tr);
- setOverview(ov);
+ setQuota(hub.quota);
+ setTrend(hub.trend);
+ setOverview(hub.overview);
+ const selectable = new Set(data.pages.filter((p) => p.status !== 'running').map((p) => p.key));
+ setSelected((prev) => new Set([...prev].filter((key) => selectable.has(key))));
+
+ const { opportunities: op, latestRun: run } = await getAuditHubExtras(brandId);
setOpps(op);
setLatestRun(run);
const oppSelectable = new Set(
(op?.opportunities ?? []).filter((o) => !o.running).map((o) => o.key),
);
setOppSelected((prev) => new Set([...prev].filter((key) => oppSelectable.has(key))));
- const selectable = new Set(data.pages.filter((p) => p.status !== 'running').map((p) => p.key));
- setSelected((prev) => new Set([...prev].filter((key) => selectable.has(key))));
};
// Deleting a run can change its page's score, status or trend, or remove
@@ -521,10 +489,17 @@ export default function SiteAuditPage() {
pages.some((p) => p.status === 'running') || (opps?.opportunities ?? []).some((o) => o.running);
useEffect(() => {
if (!anyRunning || !activeBrandId) return;
+ // A read can take longer than the interval: the next one waits for it
+ // rather than queueing behind it.
+ let reading = false;
const timer = setInterval(() => {
- reloadInPlace(activeBrandId).catch((err) =>
- console.error('Failed to refresh the audited pages:', err),
- );
+ if (reading) return;
+ reading = true;
+ reloadInPlace(activeBrandId)
+ .catch((err) => console.error('Failed to refresh the audited pages:', err))
+ .finally(() => {
+ reading = false;
+ });
}, RUNNING_POLL_MS);
return () => clearInterval(timer);
}, [anyRunning, activeBrandId]);
@@ -758,7 +733,8 @@ export default function SiteAuditPage() {
</Button>
</CardContent>
</Card>
- ) : pages.length === 0 &&
+ ) : !oppsLoading &&
+ pages.length === 0 &&
(opps?.opportunities.length ?? 0) === 0 &&
(opps?.excluded.length ?? 0) === 0 ? (
<Card>
@@ -769,12 +745,24 @@ export default function SiteAuditPage() {
</CardContent>
</Card>
) : (
- <Tabs ref={listsRef} value={tab ?? 'pages'} onValueChange={(v) => setTab(v as ListTab)}>
+ // While the inbox loads, its tab leads (it usually has pages) with a placeholder.
+ <Tabs
+ ref={listsRef}
+ value={tab ?? (oppsLoading ? 'opportunities' : 'pages')}
+ onValueChange={(v) => setTab(v as ListTab)}
+ >
<TabsList variant="line" aria-label={t('lists.label')}>
- {opps && (
+ {(opps || oppsLoading) && (
<TabsTrigger value="opportunities">
{t('lists.opportunities')}
- <TabCount count={opps.opportunities.length} />
+ {opps ? (
+ <TabCount count={opps.opportunities.length} />
+ ) : (
+ <Loader2
+ className="h-3 w-3 animate-spin text-muted-foreground"
+ aria-label={t('lists.loading')}
+ />
+ )}
</TabsTrigger>
)}
<TabsTrigger value="pages">
@@ -782,6 +770,20 @@ export default function SiteAuditPage() {
<TabCount count={pagesTotal} />
</TabsTrigger>
</TabsList>
+ {oppsLoading && !opps && (
+ <TabsContent value="opportunities" className="mt-3">
+ <Card aria-busy="true">
+ <CardContent className="space-y-3 py-4">
+ {Array.from({ length: 5 }).map((_, i) => (
+ <div key={i} className="flex items-center justify-between gap-4">
+ <Skeleton className="h-4 w-1/2" />
+ <Skeleton className="h-4 w-24" />
+ </div>
+ ))}
+ </CardContent>
+ </Card>
+ </TabsContent>
+ )}
{opps && (
<TabsContent value="opportunities" className="mt-3">
<OpportunitiesTable
diff --git a/web/src/lib/actions/audits-hub.test.ts b/web/src/lib/actions/audits-hub.test.ts
new file mode 100644
index 000000000..79cedfd6a
--- /dev/null
+++ b/web/src/lib/actions/audits-hub.test.ts
@@ -0,0 +1,96 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+/**
+ * The Site Audit hub's reads in two requests (getAuditHub, getAuditHubExtras):
+ * each one's reads run side by side, and a part the page can do without
+ * comes back null instead of failing the rest.
+ */
+
+vi.mock('@/lib/supabase/server', () => ({
+ createClient: vi.fn(async () => ({
+ auth: { getSession: async () => ({ data: { session: { access_token: 'token' } } }) },
+ })),
+}));
+
+const { getAuditHub, getAuditHubExtras } = await import('./audits');
+
+const BRAND = '33333333-3333-3333-3333-333333333333';
+
+let replies: Record<string, { status?: number; body: unknown }>;
+let started: string[];
+let release: () => void;
+
+beforeEach(() => {
+ started = [];
+ // Every request waits until the test lets them all answer.
+ const gate = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ replies = {
+ '/api/audits/pages': { body: { pages: [], total: 0, primaryHost: 'acme.com' } },
+ '/api/audits/quota': { body: { quota: { used: 1, running: 0, limit: 100, remaining: 99 } } },
+ '/api/audits/trend': { body: { primaryDomain: 'acme.com', points: [] } },
+ '/api/audits/overview': { body: { overview: { pages: 0 } } },
+ '/api/audits/opportunities': { body: { opportunities: [], excluded: [], staleDays: 90 } },
+ '/api/audits/auto-runs/latest': { body: { run: { id: 'run-1' } } },
+ };
+ vi.stubGlobal(
+ 'fetch',
+ vi.fn(async (url: string) => {
+ const { pathname } = new URL(url);
+ started.push(pathname);
+ await gate;
+ const reply = replies[pathname];
+ return new Response(JSON.stringify(reply.body), { status: reply.status ?? 200 });
+ }),
+ );
+});
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+/** Lets every request already called reach the gate. */
+const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
+
+describe('getAuditHub', () => {
+ it('asks for the pages, quota, trend and cards at once', async () => {
+ const hub = getAuditHub(BRAND);
+ await settle();
+ expect(started.sort()).toEqual([
+ '/api/audits/overview',
+ '/api/audits/pages',
+ '/api/audits/quota',
+ '/api/audits/trend',
+ ]);
+ release();
+ expect(await hub).toEqual({
+ pages: { pages: [], total: 0, primaryHost: 'acme.com' },
+ quota: { used: 1, running: 0, limit: 100, remaining: 99 },
+ trend: { primaryDomain: 'acme.com', points: [] },
+ overview: { pages: 0 },
+ });
+ });
+
+ it('leaves the cards out when they fail, but fails without the pages', async () => {
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ replies['/api/audits/overview'] = { status: 500, body: { message: 'down' } };
+ release();
+ expect((await getAuditHub(BRAND)).overview).toBeNull();
+
+ replies['/api/audits/pages'] = { status: 500, body: { message: 'pages down' } };
+ await expect(getAuditHub(BRAND)).rejects.toThrow('pages down');
+ });
+});
+
+describe('getAuditHubExtras', () => {
+ it('asks for the inbox and the latest run at once, each null when it fails', async () => {
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ replies['/api/audits/opportunities'] = { status: 500, body: { message: 'slow' } };
+ const extras = getAuditHubExtras(BRAND);
+ await settle();
+ expect(started.sort()).toEqual(['/api/audits/auto-runs/latest', '/api/audits/opportunities']);
+ release();
+ expect(await extras).toEqual({ opportunities: null, latestRun: { id: 'run-1' } });
+ });
+});
diff --git a/web/src/lib/actions/audits.ts b/web/src/lib/actions/audits.ts
index 56285d905..7984656a8 100644
--- a/web/src/lib/actions/audits.ts
+++ b/web/src/lib/actions/audits.ts
@@ -566,3 +566,57 @@ export async function getAuditPages(
const pages: AuditedPage[] = data.pages ?? [];
return { pages, total: data.total ?? pages.length, primaryHost: data.primaryHost ?? null };
}
+
+/** The Site Audit hub's main reads: the page shows once they're in. */
+export interface AuditHub {
+ pages: AuditedPages;
+ quota: AuditQuota;
+ trend: AuditTrend;
+ /** The KPI cards; null when they can't be read (the cards are left out). */
+ overview: AuditOverview | null;
+}
+
+/**
+ * The hub's main reads in one request, run side by side here. Server actions
+ * called from the page run one at a time, so separate calls added their
+ * times up: 23 s on production (2026-10-19), not the slowest read's 13.
+ */
+export async function getAuditHub(brandId: string, pageLimit = 50): Promise<AuditHub> {
+ const [pages, quota, trend, overview] = await Promise.all([
+ getAuditPages(brandId, 0, pageLimit),
+ getAuditQuota(),
+ getAuditTrend(brandId),
+ getAuditOverview(brandId).catch((err) => {
+ console.error('Failed to load the Site Audit overview:', err);
+ return null;
+ }),
+ ]);
+ return { pages, quota, trend, overview };
+}
+
+/** The hub's slower reads, which fill in after the page shows. */
+export interface AuditHubExtras {
+ /** The Opportunities inbox; null when it can't be read (its tab is left out). */
+ opportunities: AuditOpportunities | null;
+ /** The latest auto-audit run on the brand's pages; null for none, or when it can't be read. */
+ latestRun: AutoAuditRun | null;
+}
+
+/**
+ * The Opportunities inbox and the latest auto-audit run, side by side, after
+ * getAuditHub: the inbox alone took 13 s on production (2026-10-19), and the
+ * page needn't wait for it.
+ */
+export async function getAuditHubExtras(brandId: string): Promise<AuditHubExtras> {
+ const [opportunities, latestRun] = await Promise.all([
+ getAuditOpportunities(brandId).catch((err) => {
+ console.error('Failed to load the Site Audit opportunities:', err);
+ return null;
+ }),
+ getLatestAutoRun(brandId).catch((err) => {
+ console.error('Failed to load the latest auto-audit run:', err);
+ return null;
+ }),
+ ]);
+ return { opportunities, latestRun };
+}