Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cache): improved customizability #2652

Merged
merged 6 commits into from
May 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat(cache): cacheName can accept functions too
  • Loading branch information
MathurAditya724 committed May 10, 2024
commit 100c2ed2b44a13ff78c3d8998d373c2c2d2678af
5 changes: 3 additions & 2 deletions src/middleware/cache/index.ts
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ import type { Context } from '../../context'
import type { MiddlewareHandler } from '../../types'

export const cache = (options: {
cacheName: string
cacheName: string | ((c: Context) => Promise<string> | string)
wait?: boolean
cacheControl?: string
vary?: string | string[]
@@ -69,7 +69,8 @@ export const cache = (options: {

return async function cache(c, next) {
const key = c.req.url
const cache = await caches.open(options.cacheName)
const cacheName = typeof options.cacheName === "function" ? await options.cacheName(c) : options.cacheName;
const cache = await caches.open(cacheName)
const response = await cache.match(key)
if (response) {
return new Response(response.body, response)