Cache Middleware
The Cache middleware uses the Web Standard's Cache API. It caches a given response according to the Cache-Control
headers.
The Cache middleware currently supports Cloudflare Workers projects using custom domains and Deno projects using Deno 1.26+, but doesn't supports Lagon.
Please be aware that the Cache API is not currently supported by Deno Deploy. The caches
variable, which is part of the Cache API, may not be available in the Deno Deploy environment.
See Usage below for instructions on each platform.
Import
ts
import { Hono } from 'hono'
import { cache } from 'hono/cache'
import { Hono } from 'hono'
import { cache } from 'hono/cache'
ts
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { cache } from 'https://deno.land/x/hono/middleware.ts'
import { Hono } from 'https://deno.land/x/hono/mod.ts'
import { cache } from 'https://deno.land/x/hono/middleware.ts'
Usage
ts
app.get(
'*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
})
)
app.get(
'*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
})
)
ts
// Must use `wait: true` for the Deno runtime
app.get(
'*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
wait: true,
})
)
// Must use `wait: true` for the Deno runtime
app.get(
'*',
cache({
cacheName: 'my-app',
cacheControl: 'max-age=3600',
wait: true,
})
)
Options
cacheName
: string - required- The name of the cache. Can be used to store multiple caches with different identifiers.
wait
: boolean- A boolean indicating if Hono should wait for the Promise of the
cache.put
function to resolve before continuing with the request. Required to be true for the Deno environment. Default isfalse
.
- A boolean indicating if Hono should wait for the Promise of the
cacheControl
: string- A string of directives for the
Cache-Control
header. See the MDN docs for more information. When this option is not provided, noCache-Control
header is added to requests.
- A string of directives for the