From de9ad38835b318c0744b3be5356e73b205293570 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Fri, 28 Jun 2024 01:32:46 +0200 Subject: [PATCH] Added post processing for currency related content + cleaned up middleware --- src/cache/pageCache.ts | 22 ++ .../AppLayout/components/Html.astro | 3 +- .../Topbar/components/CurrencySelector.astro | 16 +- .../Previews/CollectiblePreview.astro | 15 +- src/env.d.ts | 1 - src/middleware.ts | 268 ++---------------- src/middleware/actionsHandling.ts | 57 ++++ src/middleware/commonHeaders.ts | 16 ++ src/middleware/cookieRefreshing.ts | 55 ++++ src/middleware/languageNegotiation.ts | 38 +++ src/middleware/pageCaching.ts | 27 ++ src/middleware/postProcessing.ts | 108 +++++++ src/middleware/requestTracking.ts | 9 + src/middleware/setAstroLocals.ts | 7 + src/middleware/utils.ts | 74 +++++ .../[slug]/_components/PriceInfo.astro | 23 +- src/pages/[locale]/settings/index.astro | 8 +- src/shared/analytics/analytics.ts | 1 - 18 files changed, 461 insertions(+), 287 deletions(-) create mode 100644 src/cache/pageCache.ts create mode 100644 src/middleware/actionsHandling.ts create mode 100644 src/middleware/commonHeaders.ts create mode 100644 src/middleware/cookieRefreshing.ts create mode 100644 src/middleware/languageNegotiation.ts create mode 100644 src/middleware/pageCaching.ts create mode 100644 src/middleware/postProcessing.ts create mode 100644 src/middleware/requestTracking.ts create mode 100644 src/middleware/setAstroLocals.ts create mode 100644 src/middleware/utils.ts diff --git a/src/cache/pageCache.ts b/src/cache/pageCache.ts new file mode 100644 index 0000000..f527a14 --- /dev/null +++ b/src/cache/pageCache.ts @@ -0,0 +1,22 @@ +import { getLogger } from "src/utils/logger"; + +class PageCache { + private readonly logger = getLogger("[PageCache]"); + private readonly cache = new Map(); + + get(url: string): Response | undefined { + const cachedPage = this.cache.get(url); + if (cachedPage) { + this.logger.log("Retrieved cached page for", url); + return cachedPage?.clone(); + } + return; + } + + set(url: string, response: Response) { + this.cache.set(url, response.clone()); + this.logger.log("Cached response for", url); + } +} + +export const pageCache = new PageCache(); diff --git a/src/components/AppLayout/components/Html.astro b/src/components/AppLayout/components/Html.astro index 594b4eb..6d537fc 100644 --- a/src/components/AppLayout/components/Html.astro +++ b/src/components/AppLayout/components/Html.astro @@ -8,6 +8,7 @@ import type { EndpointVideo, } from "src/shared/payload/payload-sdk"; import { contextCache } from "src/cache/contextCache"; +import { PostProcessingTags } from "src/middleware/postProcessing"; interface Props { openGraph?: @@ -47,7 +48,7 @@ const isIOS = parser.getOS().name === "iOS"; ( {`${id} (${formatCurrency(id)})`} @@ -35,7 +37,7 @@ const { currentCurrency } = Astro.locals;