-
Notifications
You must be signed in to change notification settings - Fork 215
/
cache.ts
44 lines (37 loc) · 1.35 KB
/
cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { CacheConditions, H3Event } from "../types";
/**
* Check request caching headers (`If-Modified-Since`) and add caching headers (Last-Modified, Cache-Control)
* Note: `public` cache control will be added by default
* @returns `true` when cache headers are matching. When `true` is returned, no response should be sent anymore
*/
export function handleCacheHeaders(
event: H3Event,
opts: CacheConditions,
): boolean {
const cacheControls = ["public", ...(opts.cacheControls || [])];
let cacheMatched = false;
if (opts.maxAge !== undefined) {
cacheControls.push(`max-age=${+opts.maxAge}`, `s-maxage=${+opts.maxAge}`);
}
if (opts.modifiedTime) {
const modifiedTime = new Date(opts.modifiedTime);
const ifModifiedSince = event.request.headers.get("if-modified-since");
event.response.headers.set("last-modified", modifiedTime.toUTCString());
if (ifModifiedSince && new Date(ifModifiedSince) >= opts.modifiedTime) {
cacheMatched = true;
}
}
if (opts.etag) {
event.response.headers.set("etag", opts.etag);
const ifNonMatch = event.request.headers.get("if-none-match");
if (ifNonMatch === opts.etag) {
cacheMatched = true;
}
}
event.response.headers.set("cache-control", cacheControls.join(", "));
if (cacheMatched) {
event.response.status = 304;
return true;
}
return false;
}