Skip to content

fix(use-swrv): compare serialized keys during revalidation#428

Open
officialasishkumar wants to merge 2 commits into
Kong:masterfrom
officialasishkumar:fix/serialized-key-revalidation
Open

fix(use-swrv): compare serialized keys during revalidation#428
officialasishkumar wants to merge 2 commits into
Kong:masterfrom
officialasishkumar:fix/serialized-key-revalidation

Conversation

@officialasishkumar
Copy link
Copy Markdown

Summary

  • compare mutation targets with serialized cache keys so computed array keys update refs correctly
  • keep validation/loading state tied to the current hook key when older requests resolve after a key change
  • widen cache and mutate key typing from string to the existing IKey union

Fixes #390.
Fixes #345.

Validation

  • yarn test use-swrv --runInBand
  • yarn test --runInBand
  • yarn types:check
  • yarn lint --no-fix
  • yarn build

Compare cache keys through the cache serializer when applying mutations so computed array keys and other structurally equivalent keys update their refs. Also avoid clearing validation/loading state when an older in-flight request resolves after a hook has moved to a different key.

Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 23, 2026

👷 Deploy request for docs-swrv pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 8660a69

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 604d7ac3c4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/use-swrv.ts
Comment on lines +315 to +317
if (!isSameSerializedKey(stateRef.key, keyVal, config.cache)) {
return
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Clear loading when stale request resolves after key is unset

When a request is in flight and the watched key changes to a falsy value (e.g. null), the watcher skips revalidate() and only updates isValidating, so isLoading can remain true. This new early return skips the only remaining isLoading = false path for that in-flight request, leaving consumers stuck in a perpetual loading state once the old promise resolves. This regression is triggered specifically by key transitions from truthy to falsy during an active fetch.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8660a69 by clearing isLoading when the watched key becomes falsy, with a regression test for the in-flight request case.

Signed-off-by: Asish Kumar <officialasishkumar@gmail.com>
@officialasishkumar officialasishkumar force-pushed the fix/serialized-key-revalidation branch from 71c5d9e to 8660a69 Compare April 23, 2026 20:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant