diff --git a/package.json b/package.json index cf2375f88..9252a7fbb 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "@js-temporal/polyfill": "^0.5.1", "@lezer/highlight": "^1.2.3", "@modelcontextprotocol/sdk": "^1.31.0", + "@number-flow/react": "^0.6.2", "@octokit/graphql": "^9.0.3", "@phosphor-icons/react": "^2.1.10", "@radix-ui/react-dialog": "^1.1.15", diff --git a/patches/@number-flow__react@0.6.2.patch b/patches/@number-flow__react@0.6.2.patch new file mode 100644 index 000000000..898b7e6f2 --- /dev/null +++ b/patches/@number-flow__react@0.6.2.patch @@ -0,0 +1,28 @@ +diff --git a/dist/NumberFlow-client-Cwo3BD4J.js b/dist/NumberFlow-client-Cwo3BD4J.js +--- a/dist/NumberFlow-client-Cwo3BD4J.js ++++ b/dist/NumberFlow-client-Cwo3BD4J.js +@@ -104,6 +104,10 @@ + return null; + } + componentDidUpdate(_, __, didUpdate) { ++ if (isReact19 && this.el) { ++ // Redact writes an attribute instead of assigning this property. ++ this.el.data = this.props.data; ++ } + didUpdate?.(); + } + handleRef(el) { +diff --git a/dist/NumberFlow-client-BGPmzcXX.mjs b/dist/NumberFlow-client-BGPmzcXX.mjs +--- a/dist/NumberFlow-client-BGPmzcXX.mjs ++++ b/dist/NumberFlow-client-BGPmzcXX.mjs +@@ -81,6 +81,10 @@ + return null; + } + componentDidUpdate(_, __, didUpdate) { ++ if (isReact19 && this.el) { ++ // Redact writes an attribute instead of assigning this property. ++ this.el.data = this.props.data; ++ } + didUpdate?.(); + } + handleRef(el) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f1416ea16..40d153600 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -18,6 +18,7 @@ overrides: npm-run-path@6.0.0>unicorn-magic: 0.4.0 patchedDependencies: + '@number-flow/react@0.6.2': 80306edd0e7c581978ecb77ebe3f0cfa98e3c0804130afe0df2a8ff3e1931b10 '@tanstack/openai-base@0.10.11': d5a9a6775c599c7d626961820afdcb98df388773f78274c4e3708e30cddae5d3 '@tanstack/router-core@1.171.34': f9187b6989d21f57e74b2e62f566350b92fda4efaa5af9a65ef5290137d5c27e postgres@3.4.8: 3dffa5c1d54f190ed733b23ed8c2fadd0e61c1939e5a8ba8affad5d96e13f033 @@ -89,6 +90,9 @@ importers: '@modelcontextprotocol/sdk': specifier: ^1.31.0 version: 1.31.0(@cfworker/json-schema@4.1.1)(zod@4.3.6) + '@number-flow/react': + specifier: ^0.6.2 + version: 0.6.2(patch_hash=80306edd0e7c581978ecb77ebe3f0cfa98e3c0804130afe0df2a8ff3e1931b10)(react-dom@19.2.3(react@19.2.3))(react@19.2.3) '@octokit/graphql': specifier: ^9.0.3 version: 9.0.3 @@ -2160,6 +2164,12 @@ packages: resolution: {integrity: sha512-oN+QwyX7VSHotibwubG3kpzbwKrfnyR6OOO+3Nk/53ADL7FmgHHz4TgrbaYKvvOw09u6QTx0oiH1cNCIOuN0CQ==} engines: {node: '>= 20.19.0'} + '@number-flow/react@0.6.2': + resolution: {integrity: sha512-WjZuV4aA+vhRCgCF+adGLgFVVAJ8vdvq6EchRT2FiBIgElTXtDOA3YgkcMr9UHpvpS9v4H70AB5wZv0D9jc3QA==} + peerDependencies: + react: ^18 || ^19 + react-dom: ^18 || ^19 + '@octokit/endpoint@11.0.3': resolution: {integrity: sha512-FWFlNxghg4HrXkD3ifYbS/IdL/mDHjh9QcsNyhQjN8dplUoZbejsdpmuqdA76nxj2xoWPs7p8uX2SNr9rYu0Ag==} engines: {node: '>= 20'} @@ -5755,6 +5765,9 @@ packages: resolution: {integrity: sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==} engines: {node: '>=10'} + esm-env@1.2.2: + resolution: {integrity: sha512-Epxrv+Nr/CaL4ZcFGPJIYLWFom+YeV1DqMLHJoEd9SYRxNbaFruBwfEX/kkHUJf55j2+TUbmDcmuilbP1TmXHA==} + esprima@4.0.1: resolution: {integrity: sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==} engines: {node: '>=4'} @@ -6954,6 +6967,9 @@ packages: nth-check@2.1.1: resolution: {integrity: sha512-lqjrjmaOoAnWfMmBPL+XNnynZh2+swxiX3WUE0s4yEHI6m+AwrK2UZOimIRl3X/4QctVqS8AiZjFqyOGrMXb/w==} + number-flow@0.6.2: + resolution: {integrity: sha512-MCnImG4Q5vPwhSXnov56nOuyyKn6LC+Qd7II1UiKc+ACRtug5iAtn0+CwXNxM38AC5lSowEY+oYEtZX2qMnUyw==} + object-assign@4.1.1: resolution: {integrity: sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==} engines: {node: '>=0.10.0'} @@ -10038,6 +10054,13 @@ snapshots: '@noble/hashes@2.3.0': {} + '@number-flow/react@0.6.2(patch_hash=80306edd0e7c581978ecb77ebe3f0cfa98e3c0804130afe0df2a8ff3e1931b10)(react-dom@19.2.3(react@19.2.3))(react@19.2.3)': + dependencies: + esm-env: 1.2.2 + number-flow: 0.6.2 + react: 19.2.3 + react-dom: 19.2.3(react@19.2.3) + '@octokit/endpoint@11.0.3': dependencies: '@octokit/types': 16.0.0 @@ -13810,6 +13833,8 @@ snapshots: escape-string-regexp@4.0.0: optional: true + esm-env@1.2.2: {} + esprima@4.0.1: {} esrap@2.3.2: @@ -15044,6 +15069,10 @@ snapshots: dependencies: boolbase: 1.0.0 + number-flow@0.6.2: + dependencies: + esm-env: 1.2.2 + object-assign@4.1.1: {} object-inspect@1.13.4: {} diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 464963620..1036dd4be 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -46,6 +46,7 @@ allowBuilds: electron-winstaller: true patchedDependencies: + '@number-flow/react@0.6.2': patches/@number-flow__react@0.6.2.patch '@tanstack/openai-base@0.10.11': patches/@tanstack__openai-base@0.10.11.patch '@tanstack/router-core@1.171.34': patches/@tanstack__router-core@1.171.34.patch postgres@3.4.8: patches/postgres@3.4.8.patch diff --git a/src/components/OpenSourceStats.tsx b/src/components/OpenSourceStats.tsx index 85e3d6835..ae0bed1c1 100644 --- a/src/components/OpenSourceStats.tsx +++ b/src/components/OpenSourceStats.tsx @@ -10,6 +10,8 @@ import { ossStatsQuery, recentDownloadsQuery, } from '~/queries/stats' +import { HomeStatNumber } from '~/components/home/HomeStatNumber' +import { LiveDownloadCount } from '~/components/home/LiveDownloadCount' import { useNpmDownloadCounter } from '~/hooks/useNpmDownloadCounter' import { StatsSection, @@ -19,6 +21,14 @@ import { } from '~/components/ds/ui' import { tanStackTotalNpmStatsLibrary } from '~/utils/tanstack-npm-stats' +export type HomepageStatsSnapshot = { + totalDownloads?: number + weeklyDownloads?: number + weeklyRatePerDay?: number + npmStatsFetchedAt: number + starCount?: number +} + /** Compact count with a single-letter magnitude, e.g. 2_340_000_000 → "2.3B". */ function formatCompact(value: number) { if (value >= 1_000_000_000) return `${(value / 1_000_000_000).toFixed(1)}B` @@ -42,18 +52,23 @@ export default function OssStats({ layout = 'landscape', library, page = 'home', + initialStats, }: { className?: string layout?: StatsLayout library?: Library page?: StatsPage + initialStats?: HomepageStatsSnapshot }) { const { data: stats, isLoading } = useQuery(ossStatsQuery({ library })) - const { data: homepageNpmSummary, isLoading: isLoadingHomepageNpmSummary } = - useQuery({ - ...homepageNpmStatsSummaryQuery(), - enabled: !library, - }) + const { + data: homepageNpmSummary, + dataUpdatedAt, + isLoading: isLoadingHomepageNpmSummary, + } = useQuery({ + ...homepageNpmStatsSummaryQuery(), + enabled: !library, + }) const { data: recentDownloads, isLoading: isLoadingRecentDownloads } = useQuery({ ...recentDownloadsQuery({ @@ -65,28 +80,30 @@ export default function OssStats({ const totalNpmStats = stats?.npm const npmDownloads = library ? (totalNpmStats?.totalDownloads ?? 0) - : (homepageNpmSummary?.totalDownloads ?? 0) - const starCount = stats?.github?.starCount ?? 0 + : (homepageNpmSummary?.totalDownloads ?? initialStats?.totalDownloads ?? 0) + const starCount = stats?.github?.starCount ?? initialStats?.starCount ?? 0 const weeklyDownloads = library ? (recentDownloads?.weeklyDownloads ?? 0) - : (homepageNpmSummary?.weeklyDownloads ?? 0) + : (homepageNpmSummary?.weeklyDownloads ?? + initialStats?.weeklyDownloads ?? + 0) const weeklyRatePerDay = library ? undefined - : homepageNpmSummary?.weeklyRatePerDay + : (homepageNpmSummary?.weeklyRatePerDay ?? initialStats?.weeklyRatePerDay) + const animate = page === 'hero' && !library + const liveDownloads = { + ratePerDay: weeklyRatePerDay ?? 0, + fetchedAt: dataUpdatedAt || initialStats?.npmStatsFetchedAt || 0, + } - // Live-ticking weekly counter — writes into the value node after mount. const weeklyRef = useNpmDownloadCounter({ totalDownloads: weeklyDownloads, ratePerDay: weeklyRatePerDay ?? 0, }) - const hasNpmDownloads = - !(library ? isLoading : isLoadingHomepageNpmSummary) && - isValidMetric(npmDownloads) - const hasStarCount = !isLoading && isValidMetric(starCount) - const hasWeeklyDownloads = - !(library ? isLoadingRecentDownloads : isLoadingHomepageNpmSummary) && - isValidMetric(weeklyDownloads) + const hasNpmDownloads = isValidMetric(npmDownloads) + const hasStarCount = isValidMetric(starCount) + const hasWeeklyDownloads = isValidMetric(weeklyDownloads) const loading = isLoading || !stats const npmLoading = library @@ -95,37 +112,68 @@ export default function OssStats({ const weeklyLoading = library ? isLoadingRecentDownloads || !recentDownloads : isLoadingHomepageNpmSummary || !homepageNpmSummary + const missingValue = animate ? '—' : '' const items: Array = [] - if (npmLoading || hasNpmDownloads) { + if (animate || npmLoading || hasNpmDownloads) { items.push({ key: 'total', icon: , - value: hasNpmDownloads ? formatCompact(npmDownloads) : '', + value: hasNpmDownloads ? ( + animate ? ( + = 1_000_000 ? 1 : 0, + maximumFractionDigits: npmDownloads >= 1_000_000 ? 1 : 0, + }} + /> + ) : ( + formatCompact(npmDownloads) + ) + ) : ( + missingValue + ), placeholder: '00.0B', label: 'Total Downloads', }) } - if (weeklyLoading || hasWeeklyDownloads) { + if (animate || weeklyLoading || hasWeeklyDownloads) { items.push({ key: 'weekly', icon: , - value: hasWeeklyDownloads ? weeklyDownloads.toLocaleString() : '', + value: hasWeeklyDownloads ? ( + animate ? ( + + ) : ( + weeklyDownloads.toLocaleString() + ) + ) : ( + missingValue + ), placeholder: '00,000,000', label: 'Weekly Downloads', - // Only hand the ticking counter a node once there's a real base value — - // otherwise it would write "0" over the empty placeholder while loading. - valueRef: hasWeeklyDownloads ? weeklyRef : undefined, + valueRef: hasWeeklyDownloads && !animate ? weeklyRef : undefined, }) } - if (loading || hasStarCount) { + if (animate || loading || hasStarCount) { items.push({ key: 'stars', icon: , - value: hasStarCount ? starCount.toLocaleString() : '', + value: hasStarCount ? ( + animate ? ( + + ) : ( + starCount.toLocaleString() + ) + ) : ( + missingValue + ), placeholder: '000,000', label: 'GitHub Stars', }) diff --git a/src/components/ds/ui/StatsSection.tsx b/src/components/ds/ui/StatsSection.tsx index 915c65895..ed45dcfa9 100644 --- a/src/components/ds/ui/StatsSection.tsx +++ b/src/components/ds/ui/StatsSection.tsx @@ -28,8 +28,8 @@ export type StatsLayout = 'landscape' | 'stacked' | 'stacked-landscape' export type StatItem = { /** Stable key for React lists. */ key: string - /** Pre-formatted display value, e.g. "2.2B" or "65,395,147". */ - value: string + /** Display value, e.g. "2.2B" or an animated number. */ + value: React.ReactNode /** Caption beside/beneath the value. */ label: string /** Leading (or, for stacked-landscape, top) icon. */ diff --git a/src/components/home/HomeStatNumber.tsx b/src/components/home/HomeStatNumber.tsx new file mode 100644 index 000000000..f5a3b749d --- /dev/null +++ b/src/components/home/HomeStatNumber.tsx @@ -0,0 +1,28 @@ +import NumberFlow, { type Format } from '@number-flow/react' +import { useHydrated } from '@tanstack/react-router' + +export function HomeStatNumber({ + value, + format, + transformTiming, +}: { + value: number + format?: Format + transformTiming?: EffectTiming +}) { + const hydrated = useHydrated() + + // Keep visible text in the initial document before the custom element mounts. + if (!hydrated) { + return {value.toLocaleString('en-US', format)} + } + + return ( + + ) +} diff --git a/src/components/home/HomeStatsSection.tsx b/src/components/home/HomeStatsSection.tsx index 244ccc8bf..0ca41d4c9 100644 --- a/src/components/home/HomeStatsSection.tsx +++ b/src/components/home/HomeStatsSection.tsx @@ -1,5 +1,19 @@ -import OpenSourceStats from '~/components/OpenSourceStats' +import OpenSourceStats, { + type HomepageStatsSnapshot, +} from '~/components/OpenSourceStats' -export function HomeStatsSection({ className }: { className?: string }) { - return +export function HomeStatsSection({ + className, + initialStats, +}: { + className?: string + initialStats: HomepageStatsSnapshot +}) { + return ( + + ) } diff --git a/src/components/home/LiveDownloadCount.tsx b/src/components/home/LiveDownloadCount.tsx new file mode 100644 index 000000000..f92d80046 --- /dev/null +++ b/src/components/home/LiveDownloadCount.tsx @@ -0,0 +1,45 @@ +import type { Format } from '@number-flow/react' +import { useEffect, useState } from 'react' +import { HomeStatNumber } from './HomeStatNumber' + +export function LiveDownloadCount({ + value, + ratePerDay, + fetchedAt, + format, +}: { + value: number + ratePerDay: number + fetchedAt: number + format?: Format +}) { + const [sampledAt, setSampledAt] = useState(0) + const isLive = ratePerDay > 0 && fetchedAt > 0 + + useEffect(() => { + if (!isLive) return + + const tick = () => { + if (!document.hidden) { + setSampledAt(Date.now()) + } + } + const timer = window.setInterval(tick, 1000) + document.addEventListener('visibilitychange', tick) + + return () => { + window.clearInterval(timer) + document.removeEventListener('visibilitychange', tick) + } + }, [isLive]) + + const elapsedSeconds = Math.max(0, Math.floor((sampledAt - fetchedAt) / 1000)) + + return ( + + ) +} diff --git a/src/routes/index.tsx b/src/routes/index.tsx index 822448da8..9633b42c9 100644 --- a/src/routes/index.tsx +++ b/src/routes/index.tsx @@ -21,11 +21,12 @@ import { HomeNewsletterSection } from '~/components/home/HomeNewsletterSection' import { HomeSocialProofSection } from '~/components/home/HomeSocialProofSection' import { HomeStatsFallback } from '~/components/home/HomeSectionFallbacks' import { HomeStatsSection } from '~/components/home/HomeStatsSection' +import { HomeStatNumber } from '~/components/home/HomeStatNumber' +import { LiveDownloadCount } from '~/components/home/LiveDownloadCount' import { useQuery } from '@tanstack/react-query' import { Button } from '~/components/ds/ui' import { Scarf } from '~/components/Scarf' import { useInView } from '~/hooks/useInView' -import { useNpmDownloadCounter } from '~/hooks/useNpmDownloadCounter' import { homepageNpmStatsSummaryQuery, ossStatsQuery } from '~/queries/stats' import { useLibrariesOverlay } from '~/contexts/LibrariesOverlayContext' import { fetchRecentPosts } from '~/utils/blog.functions' @@ -34,7 +35,26 @@ import { seo } from '~/utils/seo' import { getTanStackHomepageJsonLd } from '~/utils/organization-structured-data' export const Route = createFileRoute('/')({ - loader: () => fetchRecentPosts(), + loader: async ({ context: { queryClient } }) => { + const npmStatsQuery = homepageNpmStatsSummaryQuery() + const [stats, npmSummary, recentPosts] = await Promise.all([ + queryClient.ensureQueryData(ossStatsQuery()), + queryClient.ensureQueryData(npmStatsQuery), + fetchRecentPosts(), + ]) + + return { + recentPosts, + initialStats: { + totalDownloads: npmSummary?.totalDownloads ?? stats.npm?.totalDownloads, + weeklyDownloads: npmSummary?.weeklyDownloads, + weeklyRatePerDay: npmSummary?.weeklyRatePerDay, + npmStatsFetchedAt: + queryClient.getQueryState(npmStatsQuery.queryKey)?.dataUpdatedAt ?? 0, + starCount: stats.github?.starCount, + }, + } + }, head: () => ({ meta: seo({ title: 'TanStack | The open-source application stack for the web.', @@ -52,7 +72,7 @@ export const Route = createFileRoute('/')({ }) function Index() { - const recentPosts = Route.useLoaderData() + const { initialStats, recentPosts } = Route.useLoaderData() const { openLibraries } = useLibrariesOverlay() const startWithPrompt = (e: React.MouseEvent) => { @@ -77,10 +97,9 @@ function Index() { headline bottom-left, description + CTA bottom-right. The sky is always light, so text uses a mode-stable dark token (neutral-500) rather than a theme-flipping semantic. */} -
- {/* `svh`, not `dvh` — the dynamic viewport grows as iOS collapses - the URL bar mid-scroll, visibly stretching the hero. */} -
+
+ {/* The small viewport height keeps the hero stable as mobile browser chrome collapses. */} +

The{' '} @@ -137,7 +156,7 @@ function Index() { when={visible({ rootMargin: '0px' })} fallback={} > - +

@@ -669,33 +688,28 @@ function PrincipleProof({ } if (proof === 'adoption') { - return + return } return } -/** - * Production-Grade proof: the live npm download odometer shared with - * the hero stats, plus weekly volume and stars. Real numbers rather than - * adjectives — the point of the principle is scale. - */ -function AdoptionProof({ accentClassName }: { accentClassName: string }) { - const { data: summary } = useQuery(homepageNpmStatsSummaryQuery()) +function AdoptionProof() { + const { initialStats } = Route.useLoaderData() + const { data: summary, dataUpdatedAt } = useQuery( + homepageNpmStatsSummaryQuery(), + ) const { data: stats } = useQuery(ossStatsQuery()) - const totalDownloads = summary?.totalDownloads ?? 0 - const weeklyDownloads = summary?.weeklyDownloads ?? 0 - const starCount = stats?.github?.starCount ?? 0 - - // npm can be slow, rate-limited, or down — showing a literal 0 would claim - // this library has no users. Fall back to a placeholder instead. - const format = (value: number) => (value > 0 ? value.toLocaleString() : '—') - - const counterRef = useNpmDownloadCounter({ - totalDownloads, - ratePerDay: summary?.weeklyRatePerDay ?? 0, - }) + const totalDownloads = + summary?.totalDownloads ?? initialStats.totalDownloads ?? 0 + const weeklyDownloads = + summary?.weeklyDownloads ?? initialStats.weeklyDownloads ?? 0 + const starCount = stats?.github?.starCount ?? initialStats.starCount ?? 0 + const liveDownloads = { + ratePerDay: summary?.weeklyRatePerDay ?? initialStats.weeklyRatePerDay ?? 0, + fetchedAt: dataUpdatedAt || initialStats.npmStatsFetchedAt, + } return (
@@ -703,29 +717,36 @@ function AdoptionProof({ accentClassName }: { accentClassName: string }) { npm downloads - 0 ? counterRef : undefined} - className={twMerge( - 'bg-linear-to-r bg-clip-text font-mono text-3xl font-black tabular-nums text-transparent sm:text-4xl', - accentClassName, + + {totalDownloads > 0 ? ( + + ) : ( + '—' )} - > - {format(totalDownloads)}
{[ - ['per week', weeklyDownloads], - ['github stars', starCount], - ].map(([label, value]) => ( -
+ { + label: 'per week', + value: + weeklyDownloads > 0 ? ( + + ) : ( + '—' + ), + }, + { + label: 'github stars', + value: starCount > 0 ? : '—', + }, + ].map(({ label, value }) => ( +
{label} - {format(Number(value))} + {value}
))}