Skip to content

perf(topics): speed up the topic detail page — collapse 6 serialized server actions into one #312

Closed

Description

@gkhngyk

Problem

Opening a topic from the topics list — /dashboard/topics/[id] — feels slow. The page is blank/skeleton for a noticeable beat before anything renders.

Root cause (from the code)

The page is a client component that, on mount, calls six server actions through one Promise.all:

const [topics, summ, tr, sv, cmp, res] = await Promise.all([
  getTopics(activeBrandId),
  getInsightsSummary(activeBrandId, { topicId }),
  getVisibilityTrend(activeBrandId, { topicId }),
  getShareOfVoiceData(activeBrandId, { topicId }),
  getCompetitorComparison(activeBrandId, { topicId }),
  getPromptResults(activeBrandId, { topicId, limit: 50 }),
]);
  1. Server actions don't run in parallel. Even inside Promise.all, Next.js processes server actions sequentially (each is a separate POST, queued one at a time). So the wait is roughly the sum of all six, not the slowest one. This is the dominant cost.
  2. Redundant DB work. Each action independently scans prompt_results (web/src/lib/actions/tracking.ts has 6 separate from('prompt_results') scans). For a single topic, summary / trend / SoV / competitors / prompt rollup largely recompute aggregates from the same rows.
  3. One global loading flag gates the whole page — nothing paints until all six resolve (no progressive render).
  4. getTopics(brandId) fetches all topics just to read one topic's name.
  5. No caching anywhere, although aggregate tables already exist (visibility_trend_aggregates, prompt_performance_aggregates).

Proposed change

Required (the core fix)

  • Collapse the six client-side server-action calls into one server action — e.g. getTopicDetail(brandId, topicId) in a new/existing action file. Its body runs the work with a real server-side Promise.all (genuinely parallel, one round trip) and returns a single bundle { topic, summary, trend, sov, competitors, results }. The simplest correct version just calls the six existing functions inside one action — no aggregation rewrite required.
  • Fetch only the one topic (its name), not the entire topics list — query the single topic by id, or carry the name from the topics list via navigation state.

This alone removes the server-action serialization penalty and is the bulk of the win.

Optional (stretch — fine to leave for a follow-up)

  • Share the prompt_results scan: inside the consolidated action, fetch the topic's rows once and derive summary / top-weak prompts / competitor rollup from that single set instead of re-scanning.
  • Progressive render: split the single loading into per-section state so the header + KPI strip paint first and the heavier charts fill in as they arrive (improves perceived speed).
  • Caching / aggregates: read trend & summary from the existing *_aggregates tables (where not already) and/or wrap reads in a short-TTL cache.
  • Future (larger, out of scope here): convert the page to a Server Component with <Suspense> streaming. Blocked on activeBrandId currently coming from the client brand store — would need the brand id from the URL/cookie first.

Files to touch

  • web/src/app/[locale]/(dashboard)/dashboard/topics/[id]/page.tsx — call one action; optionally split loading per section.
  • web/src/lib/actions/tracking.ts and/or a new action — add the consolidated getTopicDetail.
  • web/src/lib/actions/topic.ts — single-topic lookup helper if needed.

How to test

  1. Open a topic from /dashboard/topics; the detail page renders meaningfully faster (one round trip instead of six serialized ones).
  2. Every section (KPI strip, visibility trend, SoV by platform, competitor table, top/weak prompts) shows the same numbers as before — this is a performance refactor, not a behavior change.
  3. A topic with no data still shows the correct empty states.

Notes

  • No DB schema change required for the core fix.
  • Keep the output identical — verify a few topics against the current page before/after.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions