Repository navigation
fix(citations): apply the source scope inside the URL cap, not after it - #791
Merged
Merged
Conversation
The URL tab under-reported whenever a source scope was selected. Two changes that are each right on their own combined badly: #744 caps the list at the top 2,000 by citation count, and #742 then applied the scope on the client, over the rows that arrived. So the scope filtered within the global top 2,000. Measured on the largest brand, a 30-day window: scope shown actually exist Brand 21 395 Competitors 125 2,794 And it was silent — the pager counts what it filtered, so the tab read "125 of 125". A reader could not tell "this brand has 125 competitor URLs" from "125 of them ranked in the global top two thousand". citations_urls now takes the domains a scope resolves to and cuts the set before the limit; total_urls counts the scoped set, so the tab reports the scope rather than the cap. The three scopes now partition the window exactly: 395 + 2,794 + 94,131 = 97,320. What a brand or competitor domain *is* stays in TypeScript. The caller already runs classifyDomain over every aggregated domain to build the Domains tab, and the resolver reads that result, so the rule keeps one definition instead of gaining a second one in SQL. Two parameters rather than one, departing from the issue's sketch: resolving every scope to an include list would send 17,532 domains for Third-party — the whole cited set minus fourteen. Naming the fourteen to exclude says the same thing in a request that fits in a packet. The filter sits after the aggregate, and that placement is load-bearing. Pushed into the citation scan the planner abandons the hash join for a nested loop: 4.57s against a 1.67s unscoped baseline, close to the 8s statement timeout on a warm cache. Applied to the aggregated rows it is a hash semi-join costing 11ms, and every scope lands within noise of the unscoped query. Selecting a scope now refetches the overview, since the scope has to be known before the query runs. The unscoped view every page open starts on keeps its three queries in flight, so the default load is unchanged. The Domains tab is untouched: domains come back uncapped, so filtering them client-side is exact.
gkhngyk
added a commit
that referenced
this pull request
Aug 28, 2026
…ion in prod
The Citations page and the per-URL detail page both die on load in
production with `Failed to load citation data`. Every server action in
citations.ts returns 500 before reaching its first query, all with the
same digest, while the same code works in dev.
The runtime log names the cause:
ReferenceError: CitationsSourceScope is not defined
at module evaluation (.next/server/chunks/ssr/_5c45c8f8._.js)
#791 added `export type { CitationsSourceScope }` to citations.ts for
convenience. That file is a 'use server' module, and Turbopack's
production transform lists every export clause of such a module as a
runtime value in ensureServerEntryExports:
ensureServerEntryExports([k, m, n, o, CitationsSourceScope])
The type import is erased, so the bare identifier throws at module
evaluation — taking getCitationsOverview, getCitationGaps,
brandHasCitations and getCitationUrlDetail down with it. The dev
transform doesn't do this, which is why the bug only exists in deployed
builds. Declarations (`export type X = ...`, `export interface`) are
not affected — only re-export clauses.
Nothing ever imported the re-export (the type's home is
lib/citations/scope.ts and all consumers already read it from there),
so the fix is deleting the line, plus a comment so the pattern doesn't
come back. Verified by rebuilding: the identifier is gone from every
emitted chunk and each ensureServerEntryExports list holds only real
function bindings.
7 of 12 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The URL tab under-reported whenever a source scope was selected. Two changes that are each right on their own combined badly: #744 caps the URL list at the top 2,000 by citation count, and #742 then applied the scope on the client, over the rows that arrived. So the scope filtered within the global top 2,000.
Measured on the largest brand, a 30-day window:
And it was silent. The pager counts what it filtered, so the tab read "125 of 125" — a reader could not tell this brand has 125 competitor URLs from 125 of them ranked in the global top two thousand.
citations_urlsnow takes the domains a scope resolves to and cuts the set before the limit.total_urlscounts the scoped set, so the tab reports the scope rather than the cap. The three scopes now partition the window exactly:What a brand or competitor domain is stays in TypeScript. The caller already runs
classifyDomainover every aggregated domain to build the Domains tab; the resolver reads that result. No second definition of the rule in SQL to drift from the first.Two departures from the issue's sketch, both deliberate
Two parameters, not one. Resolving every scope to an include list would mean sending 17,532 domains for Third-party — the whole cited set minus fourteen. Naming the fourteen to exclude says the same thing in a request that fits in a packet. Every scope now resolves to a handful:
p_domainsp_exclude_domainsThe filter sits after the aggregate, and that placement is load-bearing. Pushed down into the citation scan, the planner abandons the hash join for a nested loop over the matching url ids — 4.57s against a 1.67s unscoped baseline, uncomfortably close to the 8s statement timeout on a warm cache. Applied to the aggregated rows it stays a hash semi-join costing 11ms:
The cost, stated plainly
Selecting a scope now refetches the overview, because the scope has to be known before the query runs — #742's instant toggle is gone for the three narrow scopes. In exchange the unscoped view that every page open starts on keeps its three queries in flight, so the default load is unchanged.
The client-side URL filter is kept rather than deleted: between picking a scope and the refetch landing,
datastill holds the previous scope's rows, and without it the table would briefly show the old scope under the new one's name. Once the fresh rows arrive it filters nothing.The Domains tab is untouched — domains come back uncapped, so filtering them client-side is exact.
Related issue
Closes #745
Type of change
feat— New featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Migration
00076is already applied to the hosted database. It drops the old 8-argument signature before creating the 10-argument one — keeping both would make an 8-argument call ambiguous. PostgREST binds by name, so no in-flight request depends on argument position and the deploy window is safe (the same reason00072could do this).cd web && yarn test— 199 pass, including 8 new cases for the scope resolver: the empty include list that must mean "nothing" and the empty exclude list that must mean "everything", which are the two ways this reintroduces itself.gapFiltersdeliberately excludes the scope).Verified against production, as a real org member:
and as a member of a different organization: 0 rows — the security-definer guard from #780 is intact.
yarn typecheck,yarn lint(7 pre-existing warnings, 0 errors — the one oncitations/page.tsxis onmaintoo, confirmed by stashing),yarn format:checkandyarn buildall pass fromweb/.Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdyarn lintpasses (run fromweb/)yarn typecheckpasses (run fromweb/)yarn format:checkpasses (run fromweb/)supabase/schema.sqlregenerated viabash supabase/build-schema.sh