Repository navigation
feat: Treeshakable Query - #12722
feat: Treeshakable Query#12722manudeli wants to merge 11 commits into
Conversation
🦋 Changeset detectedLatest commit: b10338b The changes in this PR will be included in the next version bump. This PR includes changesets to release 25 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
|
View your CI Pipeline Execution ↗ for commit b10338b
☁️ Nx Cloud last updated this comment at |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
🚀 Changeset Version Preview23 package(s) bumped directly, 2 bumped as dependents. 🟥 Major bumps
|
size-limit report 📦
|
React Query bundle sizeGzipped Query runtimeEach row compares the Query JavaScript used by the same React app. The change is measured against v6 for that row. Negative values mean fewer bytes.
What each workload leaves outThese implementations are present in the v6 Query chunk and absent from the treeshakable chunk for the same screen.
All four screens retain default structural sharing. Both bookmark screens retain mutation support. The selection screen retains invalidation and refetching. The polling screen retains Suspense, pagination and polling. Baseline adapter and core at Query runtime sizes · minified JS, gzip and BrotliThe app entry drives tree-shaking. Each isolated ESM chunk includes the React adapter and its matching core. React and React DOM are external. Sizes are bytes of the Query chunk measured directly, with gzip level 9 and Brotli quality 11.
Largest modules in the treeshakable read-only book list chunk, by rendered size before minification: Lazy bookmarks on navigation or scrollOne React app keeps its QueryClient mounted. The book list loads at startup. React.lazy imports the same bookmarks module when the user opens the route or scrolls the shelf into view. Each Query-only file is gzipped individually at level 9. The benchmark checks module and export placement against Vite’s default production build. The cumulative row counts each URL once. The v6 book list includes the mutation cache and infinite fetch behavior. Treeshakable downloads those with bookmarks. Both apps defer the mutation hook and infinite observer until bookmarks are first rendered.
Microfrontend navigationCumulative Query JavaScript (gzip) from the initial visit to the book list through the first visit to bookmarks. Both remotes share host React and React DOM. The independent builds serve distinct Query URLs. The shared build serves one Query chunk containing the features used across both routes. Chromium checks the requested Query URLs on each visit and revisit.
Read-only imports across framework adaptersThe production ESM reader entries for Angular, Lit, Preact, React, Solid, Svelte and Vue all omit the mutation cache and observer, serial mutation queues, polling, selection, placeholder data, and the infinite and multi-query observers. Each entry includes the query client, cache and ordinary observer.
|
🎯 Changes
I want to be able to add one server-state feature to a route and pay for that feature when the route loads. A v6 app can already defer an entire bookmarks screen. Its ordinary query observer and client still reach mutation caching, polling, result projection, and bulk cache operations even when the first screen only reads a list. ESM gives a bundler the opportunity to remove unreachable code. The dependency graph determines how much it can remove.
This changes the published
@tanstack/query-coreand the Angular, Lit, Preact, React, Solid, Svelte, and Vue adapters. The client owns state. Operations are named, client-first imports. See the migration guide and the v6 imperative Query API discussion.Choosing what a component loads
In React and Preact,
useQueryrequires a descriptor. Its factory selects the observer, return type, and render behavior. A regular reader can handle errors inline without pulling in Suspense or Error Boundary handling. A paginated reader includes its page controls. A variable-length group can still useuseQueriesoruseSuspenseQueries. The other adapters retain their native entry points and use the same treeshakable core. Framework reader entriesThe predicates narrow the original result. A running Suspense query returns defined data. If the fetcher can be
skipToken, its result can be undefined. The two pagination factories areinfiniteQueryOptionsandsuspenseInfiniteQueryOptions. Typed query readers · Public type testsA selector uses ordinary function-call syntax. Given
fetchPost(id): Promise<Post>, the callback takes aPost, the reader returns its title, and the cache keeps the original post.For pagination, the same hook returns pages and the controls to load the next page.
When a heterogeneous
useQuerieslist includes a selector, preparing each entry withqueryOptionsgives the callback its input type. If an inline page fetcher depends on its inferredpageParam, a named fetcher or an explicit return type makes the page type available to the selector. Query options guide · Infinite queries guiderefetchInterval: refetchInterval(3000)brings in the scheduler.select,placeholderData, andserialScopework the same way. An ordinary React or Preact reader that needs an Error Boundary importsthrowOnErrorand setsthrowOnError: [throwOnError, true]. The second element also accepts a predicate. Structural sharing remains enabled by default. Selection and serial bookmarks · PollingWhat leaves the bundle
The Actions comment compares four React workloads to the published package entries built from the v6 baseline at
dc33f10c. Percentages use the matching Query-only chunk as their denominator. React, React DOM, and application code are outside it. The analyzer artifact lets reviewers inspect the retained modules. Measurement and browser fixturesPreview checks three Brotli budgets. Importing the full React API costs 13,365 B, up 662 B from v6, so its limit increases by 365 B to exactly 13,365 B. The minimal reader uses 5,875 B. The new paginated bookmarks check covers reading, fetching the next page and saving a bookmark. Its limit is 8,196 B. The Size Limit action lists this as a new entry. Measuring the corresponding imports on
query-v6locally gives 9,725 B, a reduction of 1,529 B (15.72%). Preview size reportCI also bundles a read-only entry for each of the seven adapters. It checks that the core query client, cache, and observer survive, and that the mutation cache, serial queues, polling, selection, placeholder data, infinite observer, and multi-query observer do not. The comment shows the framework-specific costs of hydration and optimistic fetching. Cross-framework import checks
The public
hydrate(client, { queries }, options)call still accepts query-only state, andhydratecan also restore paused mutations. React and Preact’sHydrationBoundaryonly restore queries, as does Solid’s query reader during SSR. Sending those three paths through the general function pulled inmutation,mutationCache, andserialScopein production builds. The adapters now call the query restoration function directly. CI builds both React and Preact with and without the boundary to verify that hydration comes in only where needed and mutation restoration stays out. Query hydration · PR bundle measurementsLazy routes and independent deployments
The example opens on a read-only book list. A paginated bookmarks shelf loads with
React.lazywhen the reader opens it or scrolls it into view. The fixture measures the bytes needed to show the book list, the additional download when the shelf opens, and the total for a reader who visits both. Deferring the mutation cache and infinite fetching makes the initial book list smaller and the first bookmarks visit larger. Book list and lazy-loaded bookmarksOn an ad-funded storefront, the product list and its first ad slot often share the initial load. Viewable impressions directly affect our ad revenue. If a visitor scrolls past the slot before the ad renders, we can lose that impression. The fixture measures transferred bytes. Timing, viewability, and revenue would need to be measured on a production storefront.
Independently built microfrontends can each use a small subset of Query. The browser fixture compares two self-contained remotes with a deployment that shares a single Query chunk. It checks the actual URLs requested on the first and return visits. Microfrontend fixture
Release timing
A major release is our opportunity to change method calls, result fields, and option shapes. I'd like us to consider the direction for v6 and, if it needs a longer transition, adopt it progressively in v7 or later. More imports take effort to learn. Named exports, typed factories, and versioned guidance through skills or TanStack Intent could make migration easier. I'd like small frontends to be able to keep TanStack Query, including its cache semantics, as their requirements grow. TanStack Intent
✅ Checklist
pnpm run test:pr, or these tests do not apply to this pull request.🚀 Release Impact