diff --git a/src/components/npm-stats/NPMSummary.tsx b/src/components/npm-stats/NPMSummary.tsx index 1dbb4639f..cbde3e3e5 100644 --- a/src/components/npm-stats/NPMSummary.tsx +++ b/src/components/npm-stats/NPMSummary.tsx @@ -1,11 +1,9 @@ import * as React from 'react' import { Suspense } from 'react' import { useSuspenseQuery } from '@tanstack/react-query' -import { queryOptions } from '@tanstack/react-query' import { BlankErrorBoundary } from '~/components/BlankErrorBoundary' -import { ossStatsQuery } from '~/queries/stats' +import { ossStatsQuery, recentDownloadsQuery } from '~/queries/stats' import { useNpmDownloadCounter } from '~/hooks/useNpmDownloadCounter' -import { fetchRecentDownloadStats } from '~/utils/stats-queries.functions' import type { Library } from '~/libraries' /** @@ -24,26 +22,6 @@ function formatNumber(num: number): string { return num.toLocaleString() } -/** - * Query options for recent download stats - */ -function recentDownloadsQuery(library: Library) { - return queryOptions({ - queryKey: ['npm-recent-downloads', library.id], - queryFn: () => - fetchRecentDownloadStats({ - data: { - library: { - id: library.id, - repo: library.repo, - frameworks: library.frameworks, - }, - }, - }), - staleTime: 5 * 60 * 1000, // 5 minutes - }) -} - /** * Animated counter component for all-time downloads */ @@ -110,7 +88,9 @@ function NPMSummaryContent({ library }: { library: Library }) { const { data: ossStats } = useSuspenseQuery(ossStatsQuery({ library })) // Fetch recent download stats (daily, weekly, monthly) - const { data: recentStats } = useSuspenseQuery(recentDownloadsQuery(library)) + const { data: recentStats } = useSuspenseQuery( + recentDownloadsQuery({ library }), + ) return (
diff --git a/src/utils/stats-db.server.ts b/src/utils/stats-db.server.ts index 93a46a232..cb35ed422 100644 --- a/src/utils/stats-db.server.ts +++ b/src/utils/stats-db.server.ts @@ -248,6 +248,21 @@ export async function rebuildOssStatsCache(org: string = 'tanstack') { { npm: NpmStats; packageCount: number; updatedAt?: Date } >() + // When a library declares explicit npmPackageNames, only those packages + // count toward its aggregate. This avoids counting internal sub-packages + // (e.g. start-server-core, start-plugin-core) that are co-installed as + // dependencies of a single user-facing install and would otherwise inflate + // the totals several times over. + const explicitPackagesByLibrary = new Map>() + for (const library of libraries) { + if (library.npmPackageNames?.length) { + explicitPackagesByLibrary.set( + library.id, + new Set(library.npmPackageNames), + ) + } + } + for (const pkg of packages) { if (pkg.downloads === null) { continue @@ -266,6 +281,11 @@ export async function rebuildOssStatsCache(org: string = 'tanstack') { continue } + const explicitPackages = explicitPackagesByLibrary.get(pkg.libraryId) + if (explicitPackages && !explicitPackages.has(pkg.packageName)) { + continue + } + const existing = libraryNpmStatsMap.get(pkg.libraryId) ?? { npm: { totalDownloads: 0 }, packageCount: 0,