deno.land / x / lume@v2.1.4 / middlewares / expires.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
import type { Middleware } from "../core/server.ts";
const HOUR = 3600000;const DAY = HOUR * 24;const WEEK = DAY * 7;
export interface Options { /** The default duration for unknown types */ defaultDuration: number;
/** List of types with the cache duration */ durations: Record<string, number>;}
export const defaults: Options = { defaultDuration: WEEK, durations: { "text/html": 0, "application/json": 0, "application/xml": 0, "application/atom+xml": HOUR, "application/rdf+xml": HOUR, "application/rss+xml": HOUR, },};
/** Set the Expires header for better caching */export default function expires(userOptions?: Partial<Options>): Middleware { const options = { ...defaults, ...userOptions };
return async (request, next) => { const response = await next(request); const { headers } = response; const type = headers.get("Content-Type")?.split(";").shift()?.trim(); const duration = (type && type in options.durations) ? options.durations[type] : options.defaultDuration; headers.set("Expires", new Date(Date.now() + duration).toUTCString());
return response; };}
lume

Version Info

Tagged at
7 months ago