Skip to content

Stale-while-revalidate background revalidation does not lead to updated cache entries upon receiving 304 not modified. #4596

@daan944

Description

@daan944

Bug Description

Although async behavior was added in #4492 (thank you for that 👍 ), the CacheHandler does not know what to do with 304 not modified responses and ignores them. This causes the next request to be hit the backend again, instead of the updated cache.

See interceptor/cache.js#259 and handler/cache-handler.js#254 (NOT_UNDERSTOOD_STATUS_CODES includes 304).

Reproducible By

  1. Configure undici with cache interceptor.
  2. Have a backend that replies with a 304 when if-none-match and/or if-modified-since headers are present. The reply must include cache headers, e.g. max-age=5, stale-while-revalidate=3600.
  3. Perform a request with empty cache: see 200 OK and cache entry is inserted.
  4. Wait 5s (so response is stale) and perform a new request.
  5. Cache handler is responding immediately with stale data (good!) and starts revalidation.
  6. Revalidation is being answered by backend with 304 not modified.
  7. Perform a new request
  8. See request hitting backend server, not using cache (I think). Backend server replies with 200 OK again and it is being cached again.

In my Varnish logs I see requests answered with 304 and upon next request requested again and answered with 200 (which implies this is not a revalidation request).

Expected Behavior

When revalidation request is being answered with a 304 not modified and cache headers, we expect the cache expiry to be updated for this request.

Additional context

I think that we should use the CacheRevalidationHandler in the cache interceptor (instead of the CacheHandler) and update the cache upon succesful revalidation. Similar to what is happening when we're within the stale-if-error period, see interceptor/cache.js#325.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions