Skip to content

fix(mutation): support conditional fetching by allowing falsy keys#4226

Open
DiogenesFerminS wants to merge 2 commits into
vercel:mainfrom
DiogenesFerminS:fix/mutation-null-key
Open

fix(mutation): support conditional fetching by allowing falsy keys#4226
DiogenesFerminS wants to merge 2 commits into
vercel:mainfrom
DiogenesFerminS:fix/mutation-null-key

Conversation

@DiogenesFerminS

Copy link
Copy Markdown

Description

This PR aligns the behavior of useSWRMutation with useSWR regarding conditional fetching.

Previously, providing a null key (or a function returning a falsy value) to useSWRMutation caused an internal console.error during serialization and threw a "missing key" error when calling trigger(). This behavior contradicted the SWR documentation on conditional fetching, which states that falsy keys should simply prevent the request.

Changes

  • Logic Fix: Added a guard clause in the trigger function to return early if the resolved key is falsy. This ensures consistency across the library and prevents unhandled console errors.
  • Test Update: Updated the legacy test case should error when triggering an empty key to reflect the correct documented behavior (it now expects a silent return instead of an error throw).
  • Bug Fix: Prevented the internal serialize utility from being called with invalid keys, eliminating the reported console noise.

Linked Issue

Fixes #4217

Testing

  • Verified with a new regression test for null keys.
  • Ran the full mutation test suite: pnpm test mutation. All tests passed.

Cc: @SL-YutoTakagi

@codesandbox-ci

codesandbox-ci Bot commented Feb 27, 2026

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

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

Labels

None yet

1 participant