Skip to content

Array keys from computed() fail to update data refs due to reference comparison in mutate() #390

Description

@kindy

commit: 790a22c

When using a computed() that returns an array as the key, the data ref is never updated even though the fetcher successfully resolves and the cache is correctly populated.

const id = ref('user-1')
const { data } = useSWRV(
  computed(() => ['/api/user', id.value]),  // Returns NEW array each time
  (_endpoint, userId) => fetchUser(userId)
)

In src/use-swrv.ts:128, the mutate() function uses direct reference comparison (===) to filter refs:

let refs = stateRef.data.filter(r => r.key === key)

However, when using a computed() key that returns an array, each evaluation creates a new array instance. While SWRVCache.serializeKey() correctly hashes the array (so cache lookup works), the === comparison fails because:

suggest fix:

-    let refs = stateRef.data.filter(r => r.key === key)
+    const _key = cache.serializeKey(key);
+    let refs = stateRef.data.filter(r => cache.serializeKey(r.key) === _key)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions