Repository navigation
Description
Summary
The Citations page's server-side aggregation classifies every citation occurrence — calling classifyDomain() (and classifyArticleType()) once per citation — instead of once per distinct domain/URL. On a brand with a lot of history this is the dominant CPU cost of the Citations load: the same host (say reddit.com) that appears in thousands of citations is re-classified thousands of times, and each classifyDomain call scans 8+ suffix lists (.some() over brandDomains, competitorDomains, FORUM_DOMAINS, SOCIAL_DOMAINS, REVIEW_DOMAINS, …) plus regexes.
Memoizing classification by host/URL collapses tens of thousands of calls to a few hundred (the number of distinct domains), with zero behavior change.
Root cause (from the code)
In web/src/lib/actions/citations.ts:
getCitationsOverview— nestedfor (result) { for (cite) { … } }loop calls, per citation:extractHostname(cite.url)classifyDomain(host, classifyCtx)classifyArticleType(cite.url, cite.title)- URL normalization (
new URL(...))
getCitationGaps— same nested loop;classifyDomain(host, classifyCtx)per citation (itsdomainCatmap is re-created per result, so the same host is re-classified across results).
classifyDomain (web/src/lib/citations/classify.ts) runs matchesSuffix (list.some(...)) against 8+ domain lists + a couple of regexes on every call, and classifyArticleType (article-type.ts) tests ~20 regexes per call — so the per-occurrence cost is real.
classifyCtx (brand + competitor domains) is constant for the duration of a single aggregation call, so a domain's classification is stable — memoizing it is safe.
Proposed change
Add per-call memoization keyed by the input, so each distinct value is classified once:
- A
Map<string, SourceCategory>for domain classification — look up byhost, compute viaclassifyDomain(host, classifyCtx)only on a miss. - A
Map<string, …>forclassifyArticleType, keyed bycite.url(orurl + '\n' + title). - Apply in both
getCitationsOverviewandgetCitationGaps.
Distinct domains/URLs are in the hundreds even when total citations are in the tens of thousands, so this removes the bulk of the classification work. Output is identical (same inputs → same category), so the Domains / URLs / source-type breakdown and the Competitor Gaps results are unchanged.
Explicit non-goals
- This is a pure in-memory memoization — no DB change, no migration, no behavior change.
- The separate, larger idea of pushing the
citationsjsonb unnest + grouping into SQL (so the heavy jsonb isn't transferred and only distinct domains reach JS) is out of scope here — that's a follow-up.
How to test
- On a brand with a large citation history, open
/dashboard/citations→ it loads faster (less server CPU); the Domains, URLs, and source-type breakdowns show the same numbers as before. - The Competitor Gaps tab shows the same gap domains and per-competitor map as before.
- A brand with few citations behaves exactly as today.
Notes
- Server-side (server action) change only; no schema and no UI change.
Activity
Sam-syntax35 commentedon Jul 7, 2026
i really read all the documentation ! and so far i am enjoying contributing on this project. could you please assign this to me
gkhngyk commentedon Jul 7, 2026
Thanks @Sam-syntax35, and glad you're enjoying it! You've already got a few issues in flight — let's land a couple of those first and I'll circle back to assign this one. It's a perf-sensitive change (the output has to stay identical), so it benefits from a bit more room anyway. Appreciate the enthusiasm! 🙌
d180 commentedon Jul 7, 2026
Hi @gkhngyk, looked into this. The issue is
classifyDomainandclassifyArticleTypebeing called on every citation in the loop even when the same host repeats thousands of times. Since both are pure functions andclassifyCtxis constant per call, a simpleMapcache before the loop would collapse tens of thousands of calls to a few hundred with zero behavior change. Would love to take this one, let me know if the approach sounds good!gkhngyk commentedon Jul 9, 2026
Closing as already resolved. 🎉
This landed in #375, which added host-level memoization to
web/src/lib/actions/citations.ts:domainClassificationCache = new Map<string, SourceCategory>()— domain classification is now computed once per host instead of per citationarticleTypeCache = new Map<string, ArticleType | null>()— article-type classification is memoized the same wayBoth caches are wired through the overview and gaps aggregation, so repeated hosts/URLs no longer re-run
classifyDomain/classifyArticleType. #375 didn't reference this issue in its description, so it stayed open — but the work is onmain. Nothing left to do here.