Skip to content

fix: read fallback and cacheData by own property only - #4328

Open
lenamonj wants to merge 1 commit into
vercel:mainfrom
lenamonj:own-property-config-maps
Open

fix: read fallback and cacheData by own property only#4328
lenamonj wants to merge 1 commit into
vercel:mainfrom
lenamonj:own-property-config-maps

Conversation

@lenamonj

@lenamonj lenamonj commented Sep 7, 2026

Copy link
Copy Markdown

useSWR reads config.fallback[key] and config.cacheData[key] on plain objects, so a key that names an Object.prototype member (constructor, toString, valueOf, hasOwnProperty, ...) resolves to the inherited function and the hook returns it as data. With fallback: {} in SWRConfig, useSWR('constructor', fetcher) renders data as a function instead of undefined and 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:build pass.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant