Skip to content

perf: TTL-based eviction sweep for prefetch cache#434

Open
Divkix wants to merge 2 commits intocloudflare:mainfrom
Divkix:worktree-perf/prefetch-cache-ttl-sweep
Open

perf: TTL-based eviction sweep for prefetch cache#434
Divkix wants to merge 2 commits intocloudflare:mainfrom
Divkix:worktree-perf/prefetch-cache-ttl-sweep

Conversation

@Divkix
Copy link
Contributor

@Divkix Divkix commented Mar 11, 2026

Closes #440

Summary

  • Adds a TTL sweep pass in storePrefetchResponse() that clears all expired entries before falling back to FIFO eviction, preventing expired entries from wasting cache slots on link-heavy pages
  • Exports MAX_PREFETCH_CACHE_SIZE from navigation.ts for testability
  • Adds 4 unit tests covering: all-expired sweep, FIFO fallback for fresh entries, mixed expired/fresh sweep, and no-sweep below capacity

Test plan

  • pnpm test tests/prefetch-cache.test.ts — 4/4 pass
  • pnpm test tests/shims.test.ts — 652/652 pass (no regressions)
  • pnpm run typecheck — clean
  • pnpm run lint — clean
  • pnpm run fmt:check — clean
  • CI full suite (Vitest + Playwright E2E) — all green

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.
Copilot AI review requested due to automatic review settings March 11, 2026 02:20
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 11, 2026

Open in StackBlitz

npm i https://pkg.pr.new/vinext@434

commit: e53495c

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

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_SIZE to 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.
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.

perf: TTL-based eviction sweep for prefetch cache

2 participants