queryCache.findAll() returns wrapped iterable instead of array — breaks standard invalidation APIs
#10257
Replies: 2 comments
-
|
@nicolas-chalet-wmx Thanks for the detailed environment notes (especially WebView-only repro). queryCache.findAll() returning a non-array-like wrapper in one build target strongly suggests an interop/transpilation mismatch (iterator/polyfill/runtime) rather than expected Query API behavior. Quick isolation steps:
If this confirms environment-specific iterator behavior, maintainers can likely suggest the most compatible workaround. References: |
Beta Was this translation helpful? Give feedback.
-
|
You have correctly diagnosed the root cause: module duplication. Your library and the host app are resolving to different instances of Why this happens in WebViews specificallyMobile WebViews often bundle JavaScript differently than desktop browsers or Electron. If your library is pre-bundled (e.g., shipped as a UMD/ESM bundle with react-query inlined), and the host app also has its own react-query dependency, you end up with two separate module instances. Each has its own The fix: deduplicate the moduleThe goal is to ensure both your library and the host app use the exact same Option 1: Mark react-query as a peer dependency (recommended)In your library's {
"peerDependencies": {
"@tanstack/react-query": "^5.0.0"
}
}Then configure your library's bundler to externalize react-query so it is not inlined into your bundle: // vite.config.ts (if using Vite for library mode)
export default defineConfig({
build: {
lib: { entry: "src/index.ts", formats: ["es"] },
rollupOptions: {
external: ["@tanstack/react-query", "react", "react-dom"],
},
},
});Option 2: Bundler alias (host app side)If you cannot change the library, force the host app's bundler to resolve all react-query imports to a single copy: // webpack.config.js
resolve: {
alias: {
"@tanstack/react-query": require.resolve("@tanstack/react-query"),
},
}
// vite.config.ts
resolve: {
dedupe: ["@tanstack/react-query"],
}Option 3: Stopgap (your current workaround, improved)If you need an immediate fix while sorting out the bundling: function normalizeResult<T>(result: T[] | Iterable<T>): T[] {
return Array.isArray(result) ? result : Array.from(result);
}This is what you are already doing, and it is fine as a temporary measure. But the real fix is deduplication (Options 1 or 2). To answer your specific questions
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Environment:
@tanstack/react-queryv5.90.2Description:
We're building a library that uses react-query and is consumed by a host application. In certain build configurations (mobile WebViews), we observe unexpected behavior:
We suspect this is due to module duplication where our library and the host app resolve to different instances of
@tanstack/react-query.Impact:
Due to this issue, we cannot use the standard react-query APIs for cache management:
This forces us to bypass the intended API and work directly with
Queryinstances, which feels brittle and may break with future versions.Questions:
Query.invalidate()/Query.reset()/Query.fetch()a supported pattern, or are we relying on internal implementation details?Workaround:
We currently normalize the result by checking if the first element is iterable:
Beta Was this translation helpful? Give feedback.
All reactions