From c311c0921db10127750582332dd2dc6e22152205 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Sat, 13 Jul 2024 21:12:38 +0200 Subject: [PATCH] Per-page caching control --- src/env.d.ts | 2 +- src/middleware/pageCaching.ts | 2 +- src/middleware/setAstroLocals.ts | 1 + src/pages/404.astro | 2 -- src/pages/[locale]/api/hooks/collection-operation.ts | 5 +++-- src/pages/[locale]/api/on-startup.ts | 4 +++- src/pages/[locale]/api/pages/partial.astro | 1 + src/pages/[locale]/api/timeline/partial.astro | 1 + src/pages/[locale]/search/index.astro | 2 ++ 9 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/env.d.ts b/src/env.d.ts index 94554b5..a1cdea2 100644 --- a/src/env.d.ts +++ b/src/env.d.ts @@ -4,7 +4,7 @@ declare namespace App { interface Locals { currentLocale: string; - notFound: boolean; sdkCalls: Set; + pageCaching: boolean; } } diff --git a/src/middleware/pageCaching.ts b/src/middleware/pageCaching.ts index 8a64230..ab20fdc 100644 --- a/src/middleware/pageCaching.ts +++ b/src/middleware/pageCaching.ts @@ -25,7 +25,7 @@ export const pageCachingMiddleware = defineMiddleware(async ({ url, request, loc if (response.ok) { response.headers.set("Last-Modified", new Date().toUTCString()); - if (!pathname.includes("/api/")) { + if (locals.pageCaching) { pageCache.set(pathname, response, [...locals.sdkCalls]); } } diff --git a/src/middleware/setAstroLocals.ts b/src/middleware/setAstroLocals.ts index b892e0c..eeaaead 100644 --- a/src/middleware/setAstroLocals.ts +++ b/src/middleware/setAstroLocals.ts @@ -4,5 +4,6 @@ import { getCurrentLocale } from "src/middleware/utils"; export const setAstroLocalsMiddleware = defineMiddleware(async ({ url, locals }, next) => { locals.currentLocale = getCurrentLocale(url.pathname) ?? "en"; locals.sdkCalls = new Set(); + locals.pageCaching = true; return next(); }); diff --git a/src/pages/404.astro b/src/pages/404.astro index dbbc565..e0d39b3 100644 --- a/src/pages/404.astro +++ b/src/pages/404.astro @@ -3,8 +3,6 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import Button from "components/Button.astro"; import { getI18n } from "src/i18n/i18n"; -Astro.locals.notFound = true; - const { t, getLocalizedUrl } = await getI18n(Astro.locals.currentLocale); --- diff --git a/src/pages/[locale]/api/hooks/collection-operation.ts b/src/pages/[locale]/api/hooks/collection-operation.ts index 81d4469..4a580b2 100644 --- a/src/pages/[locale]/api/hooks/collection-operation.ts +++ b/src/pages/[locale]/api/hooks/collection-operation.ts @@ -3,9 +3,10 @@ import { Collections } from "src/shared/payload/constants"; import type { AfterOperationWebHookMessage } from "src/shared/payload/webhooks"; import { contextCache, dataCache } from "src/services.ts"; -export const POST: APIRoute = async ({ request }) => { - const auth = request.headers.get("Authorization"); +export const POST: APIRoute = async ({ request, locals }) => { + locals.pageCaching = false; + const auth = request.headers.get("Authorization"); if (auth !== `Bearer ${import.meta.env.WEB_HOOK_TOKEN}`) { return new Response(null, { status: 403, statusText: "Forbidden" }); } diff --git a/src/pages/[locale]/api/on-startup.ts b/src/pages/[locale]/api/on-startup.ts index 31facaf..6052454 100644 --- a/src/pages/[locale]/api/on-startup.ts +++ b/src/pages/[locale]/api/on-startup.ts @@ -1,7 +1,9 @@ import type { APIRoute } from "astro"; import { dataCache, pageCache } from "src/services"; -export const GET: APIRoute = async () => { +export const GET: APIRoute = async ({ locals }) => { + locals.pageCaching = false; + await dataCache.init(); await pageCache.init(); return new Response(null, { status: 200, statusText: "Ok" }); diff --git a/src/pages/[locale]/api/pages/partial.astro b/src/pages/[locale]/api/pages/partial.astro index 5be470e..bf95243 100644 --- a/src/pages/[locale]/api/pages/partial.astro +++ b/src/pages/[locale]/api/pages/partial.astro @@ -13,6 +13,7 @@ import PageCredits from "./_components/PageCredits.astro"; import type { EndpointPage } from "src/shared/payload/endpoint-types"; export const partial = true; +Astro.locals.pageCaching = false; interface Props { lang?: string; diff --git a/src/pages/[locale]/api/timeline/partial.astro b/src/pages/[locale]/api/timeline/partial.astro index 7c167d3..bb347ff 100644 --- a/src/pages/[locale]/api/timeline/partial.astro +++ b/src/pages/[locale]/api/timeline/partial.astro @@ -10,6 +10,7 @@ import type { EndpointChronologyEvent } from "src/shared/payload/endpoint-types" import { payload } from "src/services"; export const partial = true; +Astro.locals.pageCaching = false; interface Props { lang?: string; diff --git a/src/pages/[locale]/search/index.astro b/src/pages/[locale]/search/index.astro index 2814bda..38bac45 100644 --- a/src/pages/[locale]/search/index.astro +++ b/src/pages/[locale]/search/index.astro @@ -18,6 +18,8 @@ import ChronologyEventPreview from "components/Previews/ChronologyEventPreview.a import { Collections } from "src/shared/payload/constants"; import { meilisearch } from "src/services"; +Astro.locals.pageCaching = false; + type State = { q: string; page: number;