swr icon indicating copy to clipboard operation
swr copied to clipboard

Bug in `createCacheHelper`

Open ghost opened this issue 1 year ago • 4 comments

Bug report

I am using useInfiniteSWR in a react SDK I have built. Everything works as expected in the development server. But when I use the production build of the SDK I am getting the following error

TypeError: Cannot read properties of undefined (reading '6')
    at Ye (index.es.js:6095:1)
    at eval (index.es.js:6468:1)
    at useSWRArgs (index.mjs:642:1)
    # other lines....

I dug deeper into this and found out that the infinite middleware in 'swr/infinite' library file uses createCacheHelper function. This function returns an array of 4 objects, of which the third object is a subscriber. This subscriber comes from state[6] where state is obtained from SWRGlobalState.get(cache).

The cache here is passed into the createCacheHelper as the first argument by the infinite middleware.

Description / Observed Behavior

This is causing my app to crash.

Expected Behavior

App should not crash.

Additional Context

SWR version - 2.2.2

Screenshot 2023-09-05 at 4 04 17 PM createCacheHelper file swr/infinite file

ghost avatar Sep 05 '23 10:09 ghost

I have the same issue. So far, I have detected that the SWRGlobalState gets deleted somehow.

Edit: It happened only in test mode using jest + React testing library. It turned out that the test was being tore down as I was not awaiting for the side-effects to run, that's why it was failing randomly.

Could it be that your component is being unmounted unexpectedly or that the fetch request is not properly aborted?

EduardoSimon avatar Nov 10 '23 17:11 EduardoSimon