Context
Hope to officially close #66 without workarounds. To be clear, I do not want to merge these changes just yet, but I want to get at least the conversation going, and a PR started so that we can implement @ecyrbe 's suggestions or our own fix.
Background
On my NextJS webpack build, and several others, we observe the following error:
Server Error
Error: No QueryClient set, use QueryClientProvider to set one
This error happened while generating the page. Any console logs will be displayed in the terminal window.
Implementing this workaround no longer works for me (for some reason):
if (options.isServer) {
config.externals = ["@tanstack/react-query", ...config.externals];
}
const reactQuery = path.resolve(require.resolve("@tanstack/react-query"));
console.log(reactQuery, 'converted ts-rest imports for react-query')
config.resolve.alias["@tanstack/react-query"] = reactQuery;
return config;
},
According to #66 and @ecyrbe, the likely culprit is that exports are not being correctly interpreted by webpack. See: https://github.com/ecyrbe/zodios/issues/191#issuecomment-1288101919.
I released a temporary test version of @ts-rest/react-query
here: https://www.npmjs.com/package/michaelangrivera-ts-rest-react-query, and I still see the same issue while implementing that suggestion.
Interestingly, though, the following works:
`const client = require('michaelangrivera-ts-rest-react-query')`
I guess this is to be expected, though, since ESM is failing and not common modules.
This is what the build looks like after nx build
for that library:
{
"name": "michaelangrivera-ts-rest-react-query",
"version": "3.6.6",
"description": "react-query client integration for @ts-rest",
"license": "MIT",
"main": "./index.cjs",
"module": "./index.js",
"typings": "index.d.ts",
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js",
"require": "./index.cjs"
},
"./package.json": "./package.json"
},
....
"peerDependencies": {
"react": "16.x.x || 17.x.x || 18.x.x",
"zod": "3.x.x",
"@tanstack/react-query": "4.x.x",
"@ts-rest/core": "3.6.0"
},
"type": "module",
"types": "./index.d.ts",
"dependencies": {}
}
I tried the following changes (I published them, re-installed, and cleared .next cache):
"generateExportsField": true,
no luck
- only using
esm
under the format
configuration, no luck
I'm wondering if changing type:
to module
in package.json
would work any magic.
Let me know if you have any suggestions, and I can implement, test them, and then update this PR @oliverbutler @ecyrbe.
I could also be totally off, and this is actual a client
instance issue with React Query and the way it's be initialized.
This whole effort may be futile as Next is moving away from webpack, but nonetheless, it may still be good to support webpack for legacy apps and people using webpack without Next.