Repository navigation
Description
Problem
Loading the Citations page is expensive by design: getCitationsOverview pages through every prompt_results row in the selected window (1000 rows per sequential PostgREST request, up to 50k rows) and classifies every citation in the server action. That cost is acceptable for a genuine window change — but today it is also paid when it buys nothing:
- Toggling a source-category filter (Exclude own domain / Competitors only / Own domain only — soon the single Sources selector from Citations: replace the three interlinked category toggle buttons with a single source-scope selector #655) triggers a full re-scan and re-download of the exact same rows. The category flags don't affect the scan at all; they only skip rows during aggregation.
- Opening the Competitor Gaps tab runs a second full scan of the same window.
On large brands with wide date ranges this turns a cheap presentational choice into a multi-second wait.
Evidence
- Sequential scan loop: web/src/lib/actions/citations.ts#L149-L214
- Category flags applied only at aggregation time: citations.ts#L295-L297 — the fetched data is identical with or without them
- Client refetches on every category-flag change because the flags are part of
apiFilters: web/src/app/%5Blocale%5D/(dashboard)/dashboard/citations/page.tsx#L858-L871 - Competitor Gaps second scan: citations.ts#L623
Proposal
Make source-scope a client-side concern. The overview payload already carries what's needed: every domain/URL row has a category (you / competitor / neutral), and sourceTypeBreakdown exists. Concretely:
- Remove the category flags from the server action's filters. The action returns the full (scope-unfiltered) aggregation for the window.
- Apply the selected scope (Citations: replace the three interlinked category toggle buttons with a single source-scope selector #655's
sourceScope) client-side: filter domain/URL rows bycategoryand derive the KPI totals for the active scope from per-category counts included in the payload. - Result: switching Sources is instant — zero network, zero re-scan. Only date/platform/topic/prompt/region changes (which genuinely change the scanned window) refetch.
This pairs naturally with #655 — implementing both together avoids wiring the new selector into the server round-trip only to remove it again.
Future work (out of scope here)
- Fetch scan pages concurrently instead of sequentially, or push the whole aggregation into a Postgres function so only aggregates cross the wire.
- Reuse the overview scan for Competitor Gaps instead of scanning the window twice.
Acceptance criteria
- Changing the source scope updates KPIs, tables, and breakdowns without any server request.
- Numbers for each scope are identical to today's server-filtered results.
- Window-changing filters (date, platform, topic, prompt, region) still refetch as before.
Activity
gkhngyk commentedon Aug 10, 2026
Coordinate with #655 before starting this — don't pick it up standalone.
#655 replaces the three category toggles with a single
sourceScopeselector, and it is assigned. This issue is what that selector should be wired to: filtering by scope on the client instead of refetching the window server-side. Doing this one first would mean building against a filter shape that #655 is about to delete; doing #655 first without this means wiring the new selector through a server round-trip only to unwire it right after.So the sensible orders are: both together in one PR, or #655 first and this immediately after. @BharadwajKanneveti has the option on combining them — if that's the route, this issue closes with that PR.
For anyone reading later, the substance hasn't changed: the overview payload already carries a
categoryon every domain and URL row, so switching scope needs no network call at all. Only date, platform, topic, prompt and region genuinely change the scanned window and should trigger a refetch.One more overlap worth knowing: #656 and #684 both touch
citations/page.tsx, so whoever lands last on that file gets a small rebase.BharadwajKanneveti commentedon Aug 13, 2026
@gkhngyk assign me this issue I will work on it
gkhngyk commentedon Aug 13, 2026
Assigned — thanks. One PR closing both #655 and #657 is the plan agreed on the other issue; put both
Closeslines in its description.The design question from #655 applies here too, since it decides what the client-side filter operates on: the new selector needs to keep "third-party" and "competitors only" apart. Neutral third-party sources — news, blogs, wikis — are visible under today's Exclude own domain and hidden under Competitors only, so a single collapsed option would lose the "who else gets cited in my space" answer.
The scan itself is unchanged by this issue: the overview payload already carries a
categoryon every domain and URL row, so switching scope should cost no network call at all. Only date, platform, topic, prompt and region genuinely change the scanned window.Start from current
main—citations/page.tsxandlib/actions/citations.tshave both moved since these issues were written (#684, #696, #707).