Skip to content

feat: Treeshakable Query - #12722

Open
manudeli wants to merge 11 commits into
query-v6from
feat/v6-treeshakable-query
Open

manudeli wants to merge 11 commits into
query-v6from
feat/v6-treeshakable-query

Conversation

@manudeli

@manudeli manudeli commented Nov 28, 2026 •

Copy link
Copy Markdown
Collaborator

🎯 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-core and 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.

await query(client, postQueryOptions(42))
await invalidateQueries(client, { queryKey: ['posts'] })

Choosing what a component loads

In React and Preact, useQuery requires 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 use useQueries or useSuspenseQueries. The other adapters retain their native entry points and use the same treeshakable core. Framework reader entries

const postQueryOptions = (id: number) => queryOptions({
  queryKey: ['post', id],
  queryFn: () => fetchPost(id), // Promise<Post>
})

function Article({ id }: { id: number }) {
  const article = useQuery(postQueryOptions(id))
  if (isSuccess(article)) return <h1>{article.data.title}</h1>
  if (isLoadingError(article)) return <p>{article.error.message}</p>
  return <Spinner />
}

const postSuspenseOptions = (id: number) => suspenseQueryOptions({
  queryKey: ['post', id],
  queryFn: () => fetchPost(id),
})

// Render under <Suspense fallback={<Spinner />}>.
function ArticleTitle({ id }: { id: number }) {
  const { data } = useQuery(postSuspenseOptions(id))
  return <h1>{data.title}</h1>
}

The 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 are infiniteQueryOptions and suspenseInfiniteQueryOptions. Typed query readers · Public type tests

A selector uses ordinary function-call syntax. Given fetchPost(id): Promise<Post>, the callback takes a Post, the reader returns its title, and the cache keeps the original post.

const postTitleOptions = (id: number) =>
  queryOptions({
    queryKey: ['post', id],
    queryFn: () => fetchPost(id),
    select: select((post) => post.title),
  })

For pagination, the same hook returns pages and the controls to load the next page.

const infiniteQueryResult = useQuery(
  infiniteQueryOptions({
    queryKey: ['projects'],
    queryFn: fetchProjects,
    initialPageParam: 0,
    getNextPageParam: (lastPage) => lastPage.nextCursor,
  }),
)

When a heterogeneous useQueries list includes a selector, preparing each entry with queryOptions gives the callback its input type. If an inline page fetcher depends on its inferred pageParam, a named fetcher or an explicit return type makes the page type available to the selector. Query options guide · Infinite queries guide

refetchInterval: refetchInterval(3000) brings in the scheduler. select, placeholderData, and serialScope work the same way. An ordinary React or Preact reader that needs an Error Boundary imports throwOnError and sets throwOnError: [throwOnError, true]. The second element also accepts a predicate. Structural sharing remains enabled by default. Selection and serial bookmarks · Polling

What 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 fixtures

React workload Features in the earlier v6 chunk that the route can omit
Read-only book list Mutation cache and serial queues, infinite fetching, polling, selection, placeholder data, optimistic fetch, query Error Boundary handling, and bulk cache operations.
Paginated bookmarks Polling, selection, placeholder data, optimistic fetch, query Error Boundary handling, serial queues, and unused bulk operations. The save action still needs mutations.
Selection, serial saves and invalidation Infinite fetching, polling, optimistic fetch, query Error Boundary handling, and bulk reads, writes, cancellation and reset. Invalidation and refetch stay.
Polling Mutation cache, serial queues, selection, placeholder data, and bulk operations. This screen uses both Suspense and pagination.

Preview 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-v6 locally gives 9,725 B, a reduction of 1,529 B (15.72%). Preview size report

CI 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, and hydrate can also restore paused mutations. React and Preact’s HydrationBoundary only restore queries, as does Solid’s query reader during SSR. Sending those three paths through the general function pulled in mutation, mutationCache, and serialScope in 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 measurements

Lazy routes and independent deployments

The example opens on a read-only book list. A paginated bookmarks shelf loads with React.lazy when 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 bookmarks

On 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

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm run test:pr, or these tests do not apply to this pull request.
  • I have followed the AI contribution policy and fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

@changeset-bot

changeset-bot Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: b10338b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 25 packages
Name Type
@tanstack/query-core Major
@tanstack/react-query Major
@tanstack/preact-query Major
@tanstack/angular-query Major
@tanstack/solid-query Major
@tanstack/svelte-query Major
@tanstack/vue-query Major
@tanstack/lit-query Major
@tanstack/query-broadcast-client-experimental Major
@tanstack/query-persist-client-core Major
@tanstack/query-devtools Major
@tanstack/query-async-storage-persister Major
@tanstack/react-query-devtools Major
@tanstack/react-query-next-experimental Major
@tanstack/react-query-persist-client Major
@tanstack/preact-query-devtools Major
@tanstack/preact-query-persist-client Major
@tanstack/angular-query-devtools Major
@tanstack/angular-query-persist-client Major
@tanstack/solid-query-devtools Major
@tanstack/solid-query-persist-client Major
@tanstack/svelte-query-devtools Major
@tanstack/svelte-query-persist-client Major
@tanstack/vue-query-devtools Major
@tanstack/eslint-plugin-query Major

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

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@nx-cloud

nx-cloud Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit b10338b

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 2m 4s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 1s View ↗

☁️ Nx Cloud last updated this comment at 2026-11-28 16:21:57 UTC

@socket-security

socket-security Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​vitest/​ui@​5.0.5991007598100
Addedplaywright@​1.65.11001001009980
Added@​vitest/​browser-playwright@​5.0.51001008698100

View full report

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

23 package(s) bumped directly, 2 bumped as dependents.

🟥 Major bumps

Package Version Reason
@tanstack/angular-query 5.108.4 → 6.0.0 Changeset
@tanstack/angular-query-devtools 5.108.4 → 6.0.0 Changeset
@tanstack/eslint-plugin-query 5.108.4 → 6.0.0 Changeset
@tanstack/lit-query 0.2.44 → 1.0.0 Changeset
@tanstack/preact-query 5.108.4 → 6.0.0 Changeset
@tanstack/preact-query-devtools 5.108.4 → 6.0.0 Changeset
@tanstack/preact-query-persist-client 5.108.4 → 6.0.0 Changeset
@tanstack/query-async-storage-persister 5.108.4 → 6.0.0 Changeset
@tanstack/query-broadcast-client-experimental 5.108.4 → 6.0.0 Changeset
@tanstack/query-core 5.108.4 → 6.0.0 Changeset
@tanstack/query-devtools 5.108.4 → 6.0.0 Changeset
@tanstack/query-persist-client-core 5.108.4 → 6.0.0 Changeset
@tanstack/react-query 5.108.4 → 6.0.0 Changeset
@tanstack/react-query-devtools 5.108.4 → 6.0.0 Changeset
@tanstack/react-query-next-experimental 5.108.4 → 6.0.0 Changeset
@tanstack/react-query-persist-client 5.108.4 → 6.0.0 Changeset
@tanstack/solid-query 5.108.4 → 6.0.0 Changeset
@tanstack/solid-query-devtools 5.108.4 → 6.0.0 Changeset
@tanstack/solid-query-persist-client 5.108.4 → 6.0.0 Changeset
@tanstack/svelte-query 6.4.7 → 7.0.0 Changeset
@tanstack/svelte-query-devtools 6.4.7 → 7.0.0 Changeset
@tanstack/vue-query 5.108.4 → 6.0.0 Changeset
@tanstack/vue-query-devtools 6.4.7 → 7.0.0 Changeset
@tanstack/angular-query-persist-client 5.108.4 → 6.0.0 Dependent
@tanstack/svelte-query-persist-client 6.4.7 → 7.0.0 Dependent

@manudeli manudeli changed the title feat: make TanStack Query v6 treeshakable across framework adapters feat: Treeshakable Query Nov 28, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
More templates

@tanstack/angular-query

npm i https://pkg.pr.new/@tanstack/angular-query@12722

@tanstack/angular-query-devtools

npm i https://pkg.pr.new/@tanstack/angular-query-devtools@12722

@tanstack/angular-query-persist-client

npm i https://pkg.pr.new/@tanstack/angular-query-persist-client@12722

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@12722

@tanstack/lit-query

npm i https://pkg.pr.new/@tanstack/lit-query@12722

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@12722

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@12722

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@12722

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@12722

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@12722

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@12722

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@12722

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@12722

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@12722

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@12722

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@12722

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@12722

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@12722

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@12722

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@12722

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@12722

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@12722

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@12722

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@12722

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@12722

commit: b10338b

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
react full 13.05 KB (+5.22% 🔺)
react minimal 5.74 KB (-29.99% 🔽)
react paginated bookmarks 8 KB (+100% 🔺)

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

React Query bundle size

Gzipped Query runtime

Each 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.

Workload React Query v6 React Query Treeshakable Change vs v6
Read-only book list 9,159 B 6,425 B -2,734 B (-29.85%)
Paginated bookmarks 10,636 B 8,927 B -1,709 B (-16.07%)
Selection, serial bookmarks and invalidation 10,320 B 9,315 B -1,005 B (-9.74%)
Polling 9,786 B 7,924 B -1,862 B (-19.03%)

What each workload leaves out

These implementations are present in the v6 Query chunk and absent from the treeshakable chunk for the same screen.

Workload Code removed compared with v6
Read-only book list Manual cached-query reset · Cached-query invalidation and observer/fetch checks · Mutation cache · Serial mutation queues · Infinite page fetching · Interval polling · Result selection and placeholder data · Optimistic observer fetch (fetchOptimistic) · Query error reset handling · Bulk cache reads, writes, cancellation and reset
Paginated bookmarks Manual cached-query reset · Cached-query invalidation and observer/fetch checks · Serial mutation queues · Interval polling · Result selection and placeholder data · Optimistic observer fetch (fetchOptimistic) · Query error reset handling · Bulk cache reads, writes, cancellation and reset
Selection, serial bookmarks and invalidation Manual cached-query reset · Infinite page fetching · Interval polling · Optimistic observer fetch (fetchOptimistic) · Query error reset handling · Bulk cache reads, writes, cancellation and reset
Polling Manual cached-query reset · Cached-query invalidation and observer/fetch checks · Mutation cache · Serial mutation queues · Result selection and placeholder data · Bulk cache reads, writes, cancellation and reset

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 dc33f10cc5d7334a3b473fdf576b5d2aeb57ef4b (base checkout). Treeshakable adapter, core and fixture at b10338b72ab7dc0a7c00a21b4ec06e191a7b9ef1.

Query runtime sizes · minified JS, gzip and Brotli

The 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.

Workload Adapter Minified JS Gzip Brotli
Read-only book list v6 29,272 B 9,159 B 8,374 B
Read-only book list treeshakable 18,721 B
-10,551 B (-36.04%)
6,425 B
-2,734 B (-29.85%)
5,889 B
-2,485 B (-29.68%)
Paginated bookmarks v6 36,362 B 10,636 B 9,712 B
Paginated bookmarks treeshakable 28,010 B
-8,352 B (-22.97%)
8,927 B
-1,709 B (-16.07%)
8,183 B
-1,529 B (-15.74%)
Selection, serial bookmarks and invalidation v6 35,099 B 10,320 B 9,429 B
Selection, serial bookmarks and invalidation treeshakable 29,353 B
-5,746 B (-16.37%)
9,315 B
-1,005 B (-9.74%)
8,547 B
-882 B (-9.35%)
Polling v6 31,491 B 9,786 B 8,955 B
Polling treeshakable 23,087 B
-8,404 B (-26.69%)
7,924 B
-1,862 B (-19.03%)
7,277 B
-1,678 B (-18.74%)

Largest modules in the treeshakable read-only book list chunk, by rendered size before minification: query-core/build/queryObserver.js, query-core/build/query.js, query-core/build/utils.js, query-core/build/retryer.js, query-core/build/queryClient.js.

Lazy bookmarks on navigation or scroll

One 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.

User action React Query v6 React Query Treeshakable Change vs v6
Initial book list load 9,279 B 6,923 B -2,356 B (-25.39%)
First bookmarks render (additional) 2,292 B 2,990 B +698 B (+30.45%)
Book list and bookmarks (cumulative) 11,571 B 9,913 B -1,658 B (-14.33%)
Return to the book list (additional, checked in Chromium) 0 B 0 B 0 B

Microfrontend navigation

Cumulative 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.

Deployment Journey React Query v6 React Query Treeshakable Change vs v6
Query per remote Book list only 9,159 B 6,425 B -2,734 B (-29.85%)
Query per remote Book list then bookmarks 19,795 B 15,352 B -4,443 B (-22.45%)
Shared Query chunk Book list only 10,636 B 8,927 B -1,709 B (-16.07%)
Shared Query chunk Book list then bookmarks 10,636 B 8,927 B -1,709 B (-16.07%)

Read-only imports across framework adapters

The 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.

Reader Hydration Infinite fetch behavior Optimistic fetch
angular Omitted Omitted Omitted
lit Omitted Omitted Included
preact Omitted Omitted Omitted
react Omitted Omitted Omitted
solid Included Included Omitted
svelte Omitted Omitted Omitted
vue Omitted Omitted Included
preact + HydrationBoundary Included Included Omitted
react + HydrationBoundary Included Included Omitted

Download the interactive bundle treemaps

@manudeli
manudeli marked this pull request as ready for review November 28, 2026 16:29
@manudeli
manudeli requested a review from a team as a code owner November 28, 2026 16:29

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant