Skip to content

fix: skip preload when the key is falsy - #4344

Open
kwy404 wants to merge 1 commit into
vercel:mainfrom
kwy404:fix/preload-skip-falsy-key
Open

kwy404 wants to merge 1 commit into
vercel:mainfrom
kwy404:fix/preload-skip-falsy-key

Conversation

@kwy404

@kwy404 kwy404 commented Oct 1, 2026

Copy link
Copy Markdown

Root cause

preload() never checks the serialized key. With a falsy key (null, false, an empty array, or a key function that throws because its dependencies are not ready), serialize() returns '' and preload still calls fetcher(fnArg), so the fetcher runs with that falsy value (for example fetch('') requests the current page). The result is also stored in PRELOAD[''], so every later preload() with a falsy key returns that same stale promise, even with a different fetcher.

useSWR treats a falsy key as "not ready" and never calls the fetcher, so preload(user ? `/api/user/${user.id}` : null, fetcher) behaves differently from the hook it is meant to warm up.

Fix

Return early from preload when the serialized key is empty, the same way it already returns undefined on the server.

Test

Added should not call the fetcher when the key is falsy to test/use-swr-preload.test.tsx. It calls preload(null, fetcher) and preload(() => { throw ... }, fetcher) and expects the fetcher not to be called. Before the fix it fails with Received number of calls: 1 (called with null); after the fix the whole preload suite passes.

@kwy404
kwy404 requested review from huozhi and shuding as code owners October 1, 2026 21:51

This branch has not been deployed

No deployments
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