urql-graphql / urql

The highly customizable and versatile GraphQL client with which you add on features like normalized caching as you grow.
https://urql.dev/goto/docs
MIT License
8.63k stars 451 forks source link

@urql/next registerUrql fails in Next.js app due to TypeError: React.cache is not a function #3467

Closed brettchalupa closed 9 months ago

brettchalupa commented 9 months ago

Describe the bug

When adding urql to a Next.js project using Jest to test the React components, the call to registerUrl (as per the docs) fails with this error: TypeError: React.cache is not a function

the error

 FAIL  app/page.test.tsx
  ● Test suite failed to run

    TypeError: React.cache is not a function

      13 | };
      14 |
    > 15 | const { getClient } = registerUrql(makeClient);
         |                                   ^
      16 |
      17 | export default function Page() {
      18 |   return <h1>App Router</h1>

      at cache (node_modules/@urql/next/src/rsc.ts:27:27)
      at Object.<anonymous> (app/page.tsx:15:35)
      at Object.<anonymous> (app/page.test.tsx:9:54)

repro steps

the error is able to reproduced in isolation by creating a new Next.js app with Jest and adding urql.

  1. npx create-next-app@latest --example with-jest with-jest-app
  2. cd with-jest-app
  3. npm install --save @urql/next urql graphql
  4. follow these steps to create and register an urql client: https://formidable.com/open-source/urql/docs/advanced/server-side-rendering/#nextjs
  5. npx jest fails with the above error

key versions

Reproduction

https://github.com/brettchalupa/next-urql-jest-react-cache-repro

Urql version

urql: 4.07 @urql/next: 1.0.1

Validations

JoviDeCroock commented 9 months ago

Ye, not sure how Next wants that to work but it injects a custom React version when running next 😅 so I think to test out server-components you might need to use the canary React version or something similar that supports React.cache.

There are some folks over here providing solutions.