perf: TTL-based eviction sweep for prefetch cache#434
Open
Divkix wants to merge 2 commits intocloudflare:mainfrom
Open
perf: TTL-based eviction sweep for prefetch cache#434Divkix wants to merge 2 commits intocloudflare:mainfrom
Divkix wants to merge 2 commits intocloudflare:mainfrom
Conversation
Before this change, expired prefetch cache entries were only cleaned on individual access or displaced by FIFO when at the 50-entry cap. On link-heavy pages, expired entries wasted slots and caused premature eviction of still-valid entries. Now storePrefetchResponse() sweeps all expired entries before falling back to FIFO eviction, so fresh entries survive longer under pressure.
commit: |
There was a problem hiding this comment.
Pull request overview
This PR improves the client-side RSC prefetch cache eviction behavior by sweeping expired cache entries (TTL-based) before falling back to FIFO eviction, and adds unit tests to validate the eviction logic.
Changes:
- Add a TTL sweep pass in
storePrefetchResponse()when the cache is at/over capacity. - Export
MAX_PREFETCH_CACHE_SIZEto make cache capacity testable. - Add new Vitest unit coverage for expired-only, fresh-only (FIFO), mixed, and below-capacity scenarios.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| tests/prefetch-cache.test.ts | Adds unit tests that exercise TTL sweep + FIFO fallback behaviors for the prefetch cache. |
| packages/vinext/src/shims/navigation.ts | Implements TTL sweep eviction and exports cache sizing constant used by tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Evicted entries were not removed from getPrefetchedUrls(), causing Link's prefetched.has() check to return true for stale URLs and skip re-prefetch. Clean up the prefetched set in both sweep and FIFO paths. Also derive test counts from MAX_PREFETCH_CACHE_SIZE constant and assert prefetched URL set consistency after eviction.
6 tasks
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.
Closes #440
Summary
storePrefetchResponse()that clears all expired entries before falling back to FIFO eviction, preventing expired entries from wasting cache slots on link-heavy pagesMAX_PREFETCH_CACHE_SIZEfromnavigation.tsfor testabilityTest plan
pnpm test tests/prefetch-cache.test.ts— 4/4 passpnpm test tests/shims.test.ts— 652/652 pass (no regressions)pnpm run typecheck— cleanpnpm run lint— cleanpnpm run fmt:check— clean