@httpland/http-cache-control
TypeScript icon, indicating that this package has built-in type declarations

1.0.0-beta.1 • Public • Published

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:

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

Package Sidebar

Install

npm i @httpland/http-cache-control

Weekly Downloads

4

Version

1.0.0-beta.1

License

MIT

Unpacked Size

87.6 kB

Total Files

53

Last publish

Collaborators

  • miyauci