Closed
Labels
enhancementNew feature or requestgood first issueGood for newcomershelp wantedExtra attention is needed
Description
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 }),
]);- 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. - Redundant DB work. Each action independently scans
prompt_results(web/src/lib/actions/tracking.tshas 6 separatefrom('prompt_results')scans). For a single topic, summary / trend / SoV / competitors / prompt rollup largely recompute aggregates from the same rows. - One global
loadingflag gates the whole page — nothing paints until all six resolve (no progressive render). getTopics(brandId)fetches all topics just to read one topic's name.- 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-sidePromise.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_resultsscan: 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
loadinginto 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
*_aggregatestables (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 onactiveBrandIdcurrently 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 splitloadingper section.web/src/lib/actions/tracking.tsand/or a new action — add the consolidatedgetTopicDetail.web/src/lib/actions/topic.ts— single-topic lookup helper if needed.
How to test
- Open a topic from
/dashboard/topics; the detail page renders meaningfully faster (one round trip instead of six serialized ones). - 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.
- 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
Labels
enhancementNew feature or requestgood first issueGood for newcomershelp wantedExtra attention is needed
Activity
perf(topics): collapse the 6 serialized topic-detail server actions i…
perf(topics): collapse 6 serialized topic-detail server actions into …
perf(insights): drop the full-table scan on load + use SQL limit/count (