← All Exercises

Decide Cache Revalidation from Headers

intermediatejavascript~25min

Background

Caching bugs appear as stale or duplicated content. Deciding from explicit directives makes CDN and browser behaviour predictable.

Problem

Parse Cache-Control directives and decide whether a conditional request returns 304, a fresh 200, or requires revalidation.

Input

JSON object: { response: { cacheControl, ageSeconds, etag? }, request: { ifNoneMatch? } }

Output

JSON object: { directives, status: 200|304, requiresRevalidation }

Sample tests

Sample 1

Input: {"response":{"cacheControl":"public, max-age=60, stale-while-revalidate=30","ageSeconds":30,"etag":"\"v1\""},"request":{"ifNoneMatch":"\"v1\""}}
Expected: {"directives":{"public":true,"maxAge":60,"staleWhileRevalidate":30},"status":304,"requiresRevalidation":false}

Sample 2

Input: {"response":{"cacheControl":"no-store","ageSeconds":0},"request":{}}
Expected: {"directives":{"noStore":true},"status":200,"requiresRevalidation":true}

Your solution

Learning objective

Predict CDN and browser cache behaviour from headers.