Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/bundlejs-api.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637/** * Cache Control Middleware * * Normalizes cache behavior for bundle endpoints. * Supports legacy paths and the `cache` query parameter. * * Flow: * +-----------+ +--------------------+ +------------------+ * | Request | ---> | Parse cache intent | ---> | Set c.var value | * +-----------+ +--------------------+ +------------------+ */
import type { CacheMode } from '../bundle/types.ts'import type { MiddlewareHandler } from 'hono'
export interface CacheControlVariables { cacheMode?: CacheMode}
const DEFAULT_CACHE_MODE: CacheMode = 'use'
export const cacheControlMiddleware: MiddlewareHandler = async (c, next) => { const url = new URL(c.req.url) const cacheParam = url.searchParams.get('cache')
let cacheMode: CacheMode = DEFAULT_CACHE_MODE
if (url.pathname === '/no-cache') { cacheMode = 'bypass' } else if (cacheParam === 'use' || cacheParam === 'bypass' || cacheParam === 'refresh') { cacheMode = cacheParam }
c.set('cacheMode', cacheMode) return await next()}