Per-page caching control

This commit is contained in:
DrMint 2024-07-13 21:12:38 +02:00
parent ba6de7244a
commit c311c0921d
9 changed files with 13 additions and 7 deletions

2
src/env.d.ts vendored
View File

@ -4,7 +4,7 @@
declare namespace App {
interface Locals {
currentLocale: string;
notFound: boolean;
sdkCalls: Set<string>;
pageCaching: boolean;
}
}

View File

@ -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]);
}
}

View File

@ -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();
});

View File

@ -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);
---

View File

@ -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" });
}

View File

@ -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" });

View File

@ -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;

View File

@ -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;

View File

@ -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;