Skip to content

Citations: source-scope changes re-scan the whole window server-side even though rows are already classified #657

Closed

Description

@gkhngyk

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:

On large brands with wide date ranges this turns a cheap presentational choice into a multi-second wait.

Evidence

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 by category and 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

  1. added
    enhancement
    on Aug 7, 2026
  2. gkhngyk commented on Aug 10, 2026

    @gkhngyk
    ContributorAuthor

    Coordinate with #655 before starting this — don't pick it up standalone.

    #655 replaces the three category toggles with a single sourceScope selector, 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 category on 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.

  3. added
    citations
    on Aug 12, 2026
  4. BharadwajKanneveti commented on Aug 13, 2026

    @BharadwajKanneveti
    Contributor

    @gkhngyk assign me this issue I will work on it

  5. gkhngyk commented on Aug 13, 2026

    @gkhngyk
    ContributorAuthor

    Assigned — thanks. One PR closing both #655 and #657 is the plan agreed on the other issue; put both Closes lines 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 category on 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.tsx and lib/actions/citations.ts have both moved since these issues were written (#684, #696, #707).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

citationsenhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions