Skip to content

httpland/http-cache-control

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

HTTP Cache-Control header middleware for standard request and response

Resources

License

Stars

Watchers

Forks