Skip to content

fix: hydrate cacheData for hooks without a fetcher - #4293

Merged
huozhi merged 1 commit into
vercel:mainfrom
yongsk0066:fix/cachedata-null-fetcher
Aug 7, 2026
Merged

huozhi merged 1 commit into
vercel:mainfrom
yongsk0066:fix/cachedata-null-fetcher

Conversation

@yongsk0066

@yongsk0066 yongsk0066 commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Hydrate request-scoped cacheData into SWR's cache for useSWR(key, null) without treating hydration as revalidation.

  • Support plain, promised, and rejected cacheData values.
  • Keep each key one-shot per request-scoped cacheData object.
  • Share consumption state across fetcher, fetcherless, and Suspense paths.
  • Avoid client fetching, validation state, retries, and revalidation callbacks during hydration.

Problem

Default-mode cacheData was consumed inside revalidate. A hook without a fetcher never enters that path, so useSWR(key, null) remained undefined and the server-loaded value was never written into SWR's cache.

Tracking consumption only by serialized key also cannot distinguish a fresh request-scoped cacheData object from an already-consumed one. Promised values additionally need protection from key changes and newer local mutations.

Solution

After React commits, a fetcherless hook resolves its matching cacheData value and writes the resulting data or error into the external cache. The commit proceeds only while:

  • the hook still owns the same key;
  • no newer mutation has started; and
  • the cache entry remains empty.

A per-hook WeakMap<CacheData, Set<string>> tracks consumption by request object and serialized key. This lets a fresh request provide new data for the same key while old request objects remain eligible for garbage collection.

Hydration does not call revalidate, so isLoading and isValidating remain false, revalidation callbacks do not fire, and no client fetcher runs.

Validation

  • Jest: 34 suites passed; 374 tests passed and 5 skipped.
  • Playwright: 7 promise-scenario tests plus dev-server warmup passed.
  • pnpm run-all-checks passed.
  • pnpm format:check passed.
  • pnpm build passed.

The browser regression verifies real RSC hydration with useSWR(key, null): the server value reaches both the hook and SWR cache, loading and validating stay false, success callbacks remain at zero, and no client request occurs.

@yongsk0066
yongsk0066 requested review from huozhi and shuding as code owners July 8, 2026 06:09
@huozhi huozhi changed the title fix: consume cacheData in hooks without a fetcher fix: hydrate cacheData for hooks without a fetcher Aug 6, 2026

@huozhi huozhi left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the PR, I tweaked the implementation a bit so you don't need the extra effect hook

@huozhi
huozhi force-pushed the fix/cachedata-null-fetcher branch from b37e080 to 2d9cf0f Compare August 6, 2026 16:30
@huozhi
huozhi merged commit ad7b276 into vercel:main Aug 7, 2026
7 checks passed
yongsk0066 added a commit to yongsk0066/yongsk0066.github.io that referenced this pull request Aug 13, 2026
* feat(about): add SWR v2.5.1 contribution to OSS list

vercel/swr#4293 — hydrate cacheData for hooks without a fetcher,
merged and shipped in SWR v2.5.1.

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PTw2dbxNkF6ribAZDV4d5d

* fix(about): correct SWR PR #4293 LOC stat to +425/-14

Verified against the squash-merge commit ad7b276 in vercel/swr
(5 files changed, 425 insertions, 14 deletions).

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01PTw2dbxNkF6ribAZDV4d5d

---------

Co-authored-by: Claude <[email protected]>
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.

2 participants