Skip to content

Conversation

@mcollina
Copy link
Member

Summary

This PR fixes the stale-while-revalidate cache directive implementation to comply with RFC 5861. Previously, it was performing synchronous revalidation, which defeated the primary purpose of reducing latency.

Fixes #4471

Changes

  • Added proper stale-while-revalidate detection: New withinStaleWhileRevalidateWindow() helper function determines if a stale response is within the revalidate window
  • Implemented background revalidation: When within the window, the cache now:
    • Returns stale content immediately (reducing latency)
    • Triggers background revalidation asynchronously using setImmediate()
    • Updates the cache silently when new content is received
  • Added comprehensive tests: Two new tests verify RFC 5861 compliance
  • Updated existing tests: Modified tests that expected the old (incorrect) synchronous behavior

Test Plan

  • Added test: stale-while-revalidate returns stale immediately and revalidates in background (RFC 5861)
  • Added test: stale-while-revalidate updates cache after background revalidation
  • Updated existing tests to expect correct behavior
  • All cache interceptor tests pass
  • Linting passes

Behavior Before/After

Before

When a cached response was stale but within the stale-while-revalidate window:

  • Client waits for revalidation request to complete
  • Response time = network latency to origin server
  • Defeats the purpose of stale-while-revalidate

After

When a cached response is stale but within the stale-while-revalidate window:

  • Client receives stale content immediately (< 10ms)
  • Background revalidation updates cache for future requests
  • Achieves the intended latency reduction benefit

RFC Compliance

This implementation follows RFC 5861 Section 3 which states:

When present in an HTTP response, the stale-while-revalidate Cache-Control extension indicates that caches MAY serve the response in which it appears after it becomes stale, up to the indicated number of seconds... while they revalidate it in the background.

🤖 Generated with Claude Code

mcollina and others added 3 commits August 31, 2025 09:37
The stale-while-revalidate cache directive was not working as intended.
Instead of returning stale content immediately and revalidating in the
background, it was performing synchronous revalidation, defeating the
primary purpose of reducing latency.

This fix:
- Returns stale content immediately when within the stale-while-revalidate window
- Performs background revalidation asynchronously to update the cache
- Updates tests to reflect the correct RFC 5861 behavior

Fixes #4471

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
Replace manual Promise creation with setTimeout from timers/promises
for cleaner and more idiomatic async delays in tests.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
…lidation

setImmediate doesn't work properly with fake timers in tests, causing
background revalidation to not trigger. process.nextTick works correctly
with both real time and fake timers.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina marked this pull request as ready for review August 31, 2025 16:55
Copy link
Member

@metcoder95 metcoder95 left a comment

Choose a reason for hiding this comment

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

lgtm

Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
Copy link
Contributor

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

RSLGTM

@mcollina mcollina merged commit 992c6d8 into main Sep 1, 2025
35 of 36 checks passed
@mcollina mcollina deleted the fix-4471 branch September 1, 2025 17:01
@github-actions github-actions bot mentioned this pull request Sep 9, 2025
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.

Cache: Stale-while-revalidate not fully working as intended by RFC5861

5 participants