From ace355b9e25235e24c21557e1a44e38a80d2a769 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 6 Apr 2022 18:11:52 +0200 Subject: [PATCH] feat: handle caching headers for cachfied handlers --- src/runtime/cache.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/runtime/cache.ts b/src/runtime/cache.ts index d0520f66f4..23427c3bb6 100644 --- a/src/runtime/cache.ts +++ b/src/runtime/cache.ts @@ -1,5 +1,5 @@ import { hash } from 'ohash' -import { H3Response, toEventHandler } from 'h3' +import { H3Response, toEventHandler, handleCacheHeaders } from 'h3' import type { CompatibilityEventHandler, CompatibilityEvent } from 'h3' import { storage } from '#nitro' @@ -16,7 +16,7 @@ export interface CachifyOptions { transform?: (entry: CacheEntry, ...args: any[]) => any group?: string integrity?: any - ttl?: number + magAge?: number swr?: boolean base?: string } @@ -25,7 +25,7 @@ const defaultCacheOptions = { name: '_', base: '/cache', swr: true, - ttl: 1 + magAge: 1 } export function defineCachedFunction (fn: ((...args) => T | Promise), opts: CachifyOptions) { @@ -42,7 +42,7 @@ export function defineCachedFunction (fn: ((...args) => T | Promise), const cacheKey = [opts.base, group, name, key].filter(Boolean).join(':') const entry: CacheEntry = await storage.getItem(cacheKey) as any || {} - const ttl = (opts.ttl ?? opts.ttl ?? 0) * 1000 + const ttl = (opts.magAge ?? opts.magAge ?? 0) * 1000 if (ttl) { entry.expires = Date.now() + ttl } @@ -108,17 +108,25 @@ export function defineCachedEventHandler (handler: CompatibilityEventHandler, op // Event already handled -_- return } + // Check for cache headers + if (handleCacheHeaders(event, { + modifiedTime: new Date(entry.mtime), + etag: `W/"${hash(entry.value)}"`, + maxAge: opts.magAge + })) { + return + } for (const header in entry.value.headers) { event.res.setHeader(header, entry.value.headers[header]) } const cacheControl = [] if (opts.swr) { - if (opts.ttl) { - cacheControl.push(`s-maxage=${opts.ttl / 1000}`) + if (opts.magAge) { + cacheControl.push(`s-maxage=${opts.magAge / 1000}`) } cacheControl.push('stale-while-revalidate') - } else if (opts.ttl) { - cacheControl.push(`max-age=${opts.ttl / 1000}`) + } else if (opts.magAge) { + cacheControl.push(`max-age=${opts.magAge / 1000}`) } if (cacheControl.length) { event.res.setHeader('Cache-Control', cacheControl.join(', '))