Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] @zodios/react with nextjs: No QueryClient set #191

Closed
QzCurious opened this issue Oct 19, 2022 · 14 comments
Closed

[Bug] @zodios/react with nextjs: No QueryClient set #191

QzCurious opened this issue Oct 19, 2022 · 14 comments

Comments

@QzCurious
Copy link
Contributor

QzCurious commented Oct 19, 2022

Steps to reproduce:

  1. clone https://github.com/QzCurious/zodios-hook-bug
  2. yarn
  3. yarn dev
  4. visit http://localhost:3000
  5. The line apiHook.useUser() produce the error
    image

It's just a simple nextjs project installed with necessary dependencies of @zodios/react. It might be easier to inspect this demo by checking what each commit does instead of reviewing whole project.

Side note: The same setup working fine with CRA on codesandbox here.

@QzCurious
Copy link
Contributor Author

Update with a CRA example on codesandbox. Sorry I can only track the problem this far.

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 19, 2022

Have you tried using Hydrate component from react query?

@QzCurious
Copy link
Contributor Author

QzCurious commented Oct 19, 2022

Not sure what is that mean.

Did you mean setting nextjs up to hydrate with react-query like using hydration?
Had follow the instruction from using hydration, it's not working either. And the instruction looks not participate in this issue though.

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 19, 2022

Hello @QzCurious ,

Thanks for taking the time to report this issue.
It seems to be related to two things. first the issue was not happening in v3 of react-query.
It's likely related to @tanstack/react-query new support of of esm modules and a bug in webpack.
This is unfortunate, but i have a workaround that i found on react-query issues

So the fix is to update webpack config of next-js to instruct it to instanciate @tanstack-query only once.
so your next.config.js should look like this :

const path = require("path");
/** @type {import('next').NextConfig} */
const nextConfig = {
  reactStrictMode: true,
  swcMinify: true,
  webpack: (config, options) => {
    if (options.isServer) {
      config.externals = ["@tanstack/react-query", ...config.externals];
    }
    const reactQuery = path.resolve(require.resolve("@tanstack/react-query"));
    config.resolve.alias["@tanstack/react-query"] = reactQuery;
    return config;
  },
};

module.exports = nextConfig;

I tested it and it works for now. i'll check with dominik the maintainer of react-query if he thinks that's fixable on @tanstack/react-query

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 19, 2022

I'll update the docs of zodios and nextjs example and add a tip for configuring webpack with this workaround in the mean time.

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 19, 2022

updated example : https://github.com/ecyrbe/zodios-express/tree/main/examples/next

update on zodios website docs incoming

@QzCurious
Copy link
Contributor Author

@ecyrbe

Thank you for always replying back, even solving, issues very soon. But I get lost in what esm, commonjs, webpack things are about. I'll try the workaround for now.
Do you mind me to report issues,if any, that could be caused by the workaround? Or it might be better to stick with react-query@3 and @zodios/core@9 for now?

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 19, 2022

I now can confirm the bug is related to esm with @tanstack/react-query since reverting to v4.2.3 or react query does not have the issue and upgrading to v4.3.0 triggers this issue

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 19, 2022

@QzCurious yes you can report any issues with the workaround. but you don't need to revert to @zodios/core v9, you can just downgrade to @tanstack/react-query v4.2.3 if you feel you don't need esm support (usually you don't).

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 19, 2022

created a bug on @tanstack/react-query : TanStack/query#4346

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 22, 2022

Closing since i updated the documentation. Not much else i can do as even with esm build just created it still needs the workaround.

@ecyrbe ecyrbe closed this as completed Oct 22, 2022
@QzCurious
Copy link
Contributor Author

For reference, the workaround is documented here.

@ecyrbe
Copy link
Owner

ecyrbe commented Oct 23, 2022

@QzCurious I finally found how to fix this on zodios packaging. I needed to had exports that are interpreted by webpack. This is now working without the workaround on :

  • @zodios/core : v10.3.1
  • @zodios/react: v10.3.2

@QzCurious
Copy link
Contributor Author

Cool, congratulations 🎉

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

No branches or pull requests

2 participants