fix: read fallback and cacheData by own property only - #4328
Open
lenamonj wants to merge 1 commit into
Open
Conversation
config.fallback and config.cacheData are plain objects, so indexing them by a key such as `constructor` or `toString` returned the inherited function and the hook handed it back as data. Read both by own property only; an own entry under such a name is still delivered.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
useSWRreadsconfig.fallback[key]andconfig.cacheData[key]on plain objects, so a key that names anObject.prototypemember (constructor,toString,valueOf,hasOwnProperty, ...) resolves to the inherited function and the hook returns it asdata. Withfallback: {}inSWRConfig,useSWR('constructor', fetcher)rendersdataas a function instead ofundefinedand never shows the fetched value as the initial state.Both reads now go through a small own-property helper. An own entry under such a name is still delivered; nothing changes for other keys.
Three tests in
use-swr-config.test.tsx: fallback and cacheData with a prototype-named key, and the own-property control. The first two fail on main.pnpm build,pnpm run-all-checks,pnpm attw,pnpm test,pnpm test:buildpass.