Openstatus
www.openstatus.dev
1interface CacheControlInfo {
2 directive: string; // unparsed directive
3 description: string;
4 name: string;
5 value?: number;
6}
7
8export function parseCacheControlHeader(header: string): CacheControlInfo[] {
9 const cacheControlDirectives = header
10 .split(",")
11 .map((directive) => directive.trim());
12
13 const cacheControlInfo: CacheControlInfo[] = [];
14
15 // biome-ignore lint/complexity/noForEach: <explanation>
16 cacheControlDirectives.forEach((directive) => {
17 const parts = directive.split("=");
18
19 const name = parts[0].trim();
20 const value = !Number.isNaN(Number(parts[1]))
21 ? Number(parts[1])
22 : undefined;
23 const description = getDirectiveDescription(name);
24
25 cacheControlInfo.push({ description, name, value, directive });
26 });
27
28 return cacheControlInfo;
29}
30
31function getDirectiveDescription(key: string): string {
32 switch (key.toLowerCase()) {
33 case "max-age":
34 return "Specifies the maximum amount of time in seconds that a resource is considered fresh in a cache. After this time, the cache is required to revalidate the resource with the origin server.";
35 case "max-stale":
36 return "Indicates that a cache can serve a stale response even after its freshness lifetime has expired. Optionally, a value can be specified to indicate the maximum staleness allowed.";
37 case "min-fresh":
38 return "Specifies the minimum amount of time in seconds that a resource must be considered fresh. Caches should not serve a response that is older than this value without revalidating with the origin server.";
39 case "s-maxage":
40 return "Similar to max-age, but applies specifically to shared caches, such as proxy servers. Overrides max-age when present in shared caches.";
41 case "no-cache":
42 return "Forces caches to submit the request to the origin server for validation before releasing a cached copy. The response must be validated with the origin server before it can be used.";
43 case "no-store":
44 return "Instructs caches not to store any part of either the request or response. The content must be obtained from the origin server for each request.";
45 case "no-transform":
46 return "Specifies that intermediaries should not modify the content of the resource, such as by transcoding or compressing it.";
47 case "only-if-cached":
48 return "Indicates that a cache should respond only if the resource is available in the cache. It should not contact the origin server to retrieve the resource.";
49 case "must-revalidate":
50 return "Specifies that caches must revalidate stale responses with the origin server before using them.";
51 case "proxy-revalidate":
52 return "Similar to must-revalidate, but applies specifically to shared caches. It indicates that shared caches must revalidate stale responses with the origin server before using them.";
53 case "private":
54 return "Indicates that the response is intended for a single user and should not be cached by shared caches.";
55 case "public":
56 return "Specifies that the response may be cached by any cache, including both private and shared caches.";
57 case "immutable":
58 return "Indicates that the response body will not change over time. Caches can store immutable responses indefinitely.";
59 case "stale-while-revalidate":
60 return "Allows a cache to serve stale responses while asynchronously revalidating them with the origin server in the background.";
61 case "stale-if-error":
62 return "Allows a cache to serve stale responses if the origin server is unavailable or returns an error.";
63 default:
64 return "-";
65 }
66}