Skip to content

feat(cache): add onRevalidate callback for SWR completion notification#69

Merged
marcstraube merged 1 commit into
masterfrom
feat/cache-revalidate-notify
Apr 19, 2026
Merged

feat(cache): add onRevalidate callback for SWR completion notification#69
marcstraube merged 1 commit into
masterfrom
feat/cache-revalidate-notify

Conversation

@marcstraube

Copy link
Copy Markdown
Owner

Summary

Closes #8

  • Add optional onRevalidate callback to CacheGetOptions<T>, invoked with fresh data when background stale-while-revalidate fetch completes
  • Enables reactive UIs to update when revalidation finishes without polling

Usage

const result = await cache.get('api:users', {
  staleWhileRevalidate: true,
  revalidate: () => fetch('/api/users').then((r) => r.json()),
  onRevalidate: (freshData) => {
    renderUsers(freshData);
    hideRefreshIndicator();
  },
});

The callback is not called when:

  • Entry is not stale (no revalidation needed)
  • Revalidation throws (stale data remains)
  • Cache is destroyed before completion
  • Revalidation is deduplicated (only first caller's callback fires)

Test plan

  • 5 new tests covering callback invocation, error case, destroy, non-stale, deduplication
  • All 4228 tests pass, 100% line coverage
  • TypeScript strict, ESLint 0 warnings
  • Documentation updated in documentation/cache.md

🤖 Generated with Claude Code

Add optional onRevalidate callback to CacheGetOptions, invoked with
fresh data when background stale-while-revalidate fetch completes.
Enables reactive UIs to update when revalidation finishes.

The callback is not invoked when revalidation fails (stale data
remains), the cache is destroyed before completion, or the entry
is not stale.

Closes #8

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@marcstraube marcstraube enabled auto-merge (squash) April 19, 2026 09:02
@marcstraube marcstraube disabled auto-merge April 19, 2026 09:16
@marcstraube marcstraube enabled auto-merge (squash) April 19, 2026 09:19
@marcstraube marcstraube merged commit ed24830 into master Apr 19, 2026
15 checks passed
@marcstraube marcstraube deleted the feat/cache-revalidate-notify branch April 19, 2026 09:35
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.

Add stale-while-revalidate completion notification

1 participant