From 0c2fe9d58bde7c0477f056a9204c713494517202 Mon Sep 17 00:00:00 2001 From: dangered wolf Date: Sun, 12 Nov 2023 02:08:33 -0500 Subject: [PATCH] Improve caching quality for non-Discord non Telegram bots --- src/caches.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/caches.ts b/src/caches.ts index 89c7a50..e967e64 100644 --- a/src/caches.ts +++ b/src/caches.ts @@ -7,13 +7,15 @@ export const cacheMiddleware = (): MiddlewareHandler => async (c, next) => { const request = c.req; const userAgent = request.header('User-Agent') ?? ''; // https://developers.cloudflare.com/workers/examples/cache-api/ - const cacheUrl = new URL( - userAgent.includes('Telegram') - ? `${request.url}&telegram` - : userAgent.includes('Discord') - ? `${request.url}&discord` - : request.url - ); + let cacheUrl = new URL(request.url); + + if (userAgent.includes('Telegram')) { + cacheUrl = new URL(`${request.url}&telegram`); + } else if (userAgent.includes('Discord')) { + cacheUrl = new URL(`${request.url}&discord`); + } else if (userAgent.match(Constants.BOT_UA_REGEX)) { + cacheUrl = new URL(`${request.url}&bot`); + } console.log('cacheUrl', cacheUrl);