From 2d6164d1f7fc813fe82751cafc23e41a82b74314 Mon Sep 17 00:00:00 2001 From: Marvin Date: Mon, 23 Dec 2024 15:00:15 +0100 Subject: [PATCH] feat: make buildCacheKey function async --- src/runtime/server/handler/serveCachedRoute.ts | 2 +- src/runtime/server/hooks/afterResponse.ts | 2 +- src/runtime/types.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runtime/server/handler/serveCachedRoute.ts b/src/runtime/server/handler/serveCachedRoute.ts index 286e172..102b4fc 100644 --- a/src/runtime/server/handler/serveCachedRoute.ts +++ b/src/runtime/server/handler/serveCachedRoute.ts @@ -49,7 +49,7 @@ export async function serveCachedHandler(event: H3Event) { // Build the cache key. const fullKey = serverOptions?.route?.buildCacheKey - ? serverOptions.route.buildCacheKey(event) + ? await serverOptions.route.buildCacheKey(event) : getCacheKeyWithPrefix(encodeRouteCacheKey(event.path), event) // Check if there is a cache entry for this key. diff --git a/src/runtime/server/hooks/afterResponse.ts b/src/runtime/server/hooks/afterResponse.ts index 94466d5..cfc5d60 100644 --- a/src/runtime/server/hooks/afterResponse.ts +++ b/src/runtime/server/hooks/afterResponse.ts @@ -89,7 +89,7 @@ export async function onAfterResponse( } const cacheKey = serverOptions?.route?.buildCacheKey - ? serverOptions.route.buildCacheKey(event) + ? await serverOptions.route.buildCacheKey(event) : getCacheKeyWithPrefix(encodeRouteCacheKey(event.path), event) const expires = routeHelper.getExpires('maxAge') diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 9b2c5f5..71a4c1b 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -192,7 +192,7 @@ export type MultiCacheServerOptions = { /** * Provide a custom function that builds the cache key for a route. */ - buildCacheKey?: (event: H3Event) => string + buildCacheKey?: (event: H3Event) => string | Promise /** * Alter the headers that are stored in the cache.