@httpland/http-cache-control
v1.0.0-beta.1
Published
HTTP Cache-Control header middleware for standard request and response
Downloads
11
Maintainers
Readme
http-cache-control
HTTP Cache-Control
header middleware for standard Request
and Response
.
What
Middleware for HTTP Content-Control
headers.
Assists in defining type-safe cache directives and safely adds Cache-Control
headers.
Compliant:
- RFC 9111, 5.2. Cache-Control
- RFC 8246, HTTP Immutable Responses
- RFC 5861, HTTP Cache-Control Extensions for Stale Content
Middleware
For a definition of Universal HTTP middleware, see the http-middleware project.
Usage
Middleware factory is exported by default.
import cacheControl from "https://deno.land/x/http_cache_control@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const middleware = cacheControl({
"max-age": 604800,
immutable: true,
public: true,
});
declare const request: Request;
const handler = () => new Response("hello");
const response = await middleware(request, handler);
assertEquals(
response.headers.get("cache-control"),
"max-age=604800, immutable, public",
);
Deep dive
If the Response
already has a Cache-Control
header, do nothing.
import cacheControl from "https://deno.land/x/http_cache_control@$VERSION/mod.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
const middleware = cacheControl({
"max-age": 604800,
immutable: true,
public: true,
});
declare const request: Request;
const handler = () =>
new Response("hello", { headers: { "cache-control": "no-cache" } });
const response = await middleware(request, handler);
assertEquals(response.headers.get("cache-control"), "no-cache");
Throwing error
If a number other than non-negative integer is specified, AggregateError
will
be thrown.
import cacheControl from "https://deno.land/x/http_cache_control@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";
assertThrows(() => cacheControl({ "max-age": -100, "s-maxage": 1.1 }));
License
Copyright © 2023-present httpland.
Released under the MIT license