easycodingjavascript~8 min
Parse a Cache-Control Header
Objective: Read HTTP caching directives (max-age, no-store, immutable) the way a browser or CDN does.
Write `parseCacheControl(header)` that turns a Cache-Control header into an object. Flags become `true`, numeric directives become numbers.
- Split on commas and trim whitespace.
- Directive names are case-insensitive: lowercase them.
- `name=value` with a numeric value becomes a number; a bare name becomes true.
- An empty or missing header returns {}.
parseCacheControl("public, max-age=3600")
→ { public: true, "max-age": 3600 }
JavaScript
⌘/Ctrl + Enter to run tests · Esc then Tab leaves the editor
Output
> Nothing has run yet.
Interview follow-up questions
- What is the difference between no-cache and no-store?
- When does `immutable` let a browser skip revalidation?
- How do s-maxage and max-age differ for a CDN?