Per-page caching control
This commit is contained in:
parent
ba6de7244a
commit
c311c0921d
|
@ -4,7 +4,7 @@
|
||||||
declare namespace App {
|
declare namespace App {
|
||||||
interface Locals {
|
interface Locals {
|
||||||
currentLocale: string;
|
currentLocale: string;
|
||||||
notFound: boolean;
|
|
||||||
sdkCalls: Set<string>;
|
sdkCalls: Set<string>;
|
||||||
|
pageCaching: boolean;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ export const pageCachingMiddleware = defineMiddleware(async ({ url, request, loc
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
response.headers.set("Last-Modified", new Date().toUTCString());
|
response.headers.set("Last-Modified", new Date().toUTCString());
|
||||||
|
|
||||||
if (!pathname.includes("/api/")) {
|
if (locals.pageCaching) {
|
||||||
pageCache.set(pathname, response, [...locals.sdkCalls]);
|
pageCache.set(pathname, response, [...locals.sdkCalls]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,5 +4,6 @@ import { getCurrentLocale } from "src/middleware/utils";
|
||||||
export const setAstroLocalsMiddleware = defineMiddleware(async ({ url, locals }, next) => {
|
export const setAstroLocalsMiddleware = defineMiddleware(async ({ url, locals }, next) => {
|
||||||
locals.currentLocale = getCurrentLocale(url.pathname) ?? "en";
|
locals.currentLocale = getCurrentLocale(url.pathname) ?? "en";
|
||||||
locals.sdkCalls = new Set();
|
locals.sdkCalls = new Set();
|
||||||
|
locals.pageCaching = true;
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
|
|
|
@ -3,8 +3,6 @@ import AppLayout from "components/AppLayout/AppLayout.astro";
|
||||||
import Button from "components/Button.astro";
|
import Button from "components/Button.astro";
|
||||||
import { getI18n } from "src/i18n/i18n";
|
import { getI18n } from "src/i18n/i18n";
|
||||||
|
|
||||||
Astro.locals.notFound = true;
|
|
||||||
|
|
||||||
const { t, getLocalizedUrl } = await getI18n(Astro.locals.currentLocale);
|
const { t, getLocalizedUrl } = await getI18n(Astro.locals.currentLocale);
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|
|
@ -3,9 +3,10 @@ import { Collections } from "src/shared/payload/constants";
|
||||||
import type { AfterOperationWebHookMessage } from "src/shared/payload/webhooks";
|
import type { AfterOperationWebHookMessage } from "src/shared/payload/webhooks";
|
||||||
import { contextCache, dataCache } from "src/services.ts";
|
import { contextCache, dataCache } from "src/services.ts";
|
||||||
|
|
||||||
export const POST: APIRoute = async ({ request }) => {
|
export const POST: APIRoute = async ({ request, locals }) => {
|
||||||
const auth = request.headers.get("Authorization");
|
locals.pageCaching = false;
|
||||||
|
|
||||||
|
const auth = request.headers.get("Authorization");
|
||||||
if (auth !== `Bearer ${import.meta.env.WEB_HOOK_TOKEN}`) {
|
if (auth !== `Bearer ${import.meta.env.WEB_HOOK_TOKEN}`) {
|
||||||
return new Response(null, { status: 403, statusText: "Forbidden" });
|
return new Response(null, { status: 403, statusText: "Forbidden" });
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import type { APIRoute } from "astro";
|
import type { APIRoute } from "astro";
|
||||||
import { dataCache, pageCache } from "src/services";
|
import { dataCache, pageCache } from "src/services";
|
||||||
|
|
||||||
export const GET: APIRoute = async () => {
|
export const GET: APIRoute = async ({ locals }) => {
|
||||||
|
locals.pageCaching = false;
|
||||||
|
|
||||||
await dataCache.init();
|
await dataCache.init();
|
||||||
await pageCache.init();
|
await pageCache.init();
|
||||||
return new Response(null, { status: 200, statusText: "Ok" });
|
return new Response(null, { status: 200, statusText: "Ok" });
|
||||||
|
|
|
@ -13,6 +13,7 @@ import PageCredits from "./_components/PageCredits.astro";
|
||||||
import type { EndpointPage } from "src/shared/payload/endpoint-types";
|
import type { EndpointPage } from "src/shared/payload/endpoint-types";
|
||||||
|
|
||||||
export const partial = true;
|
export const partial = true;
|
||||||
|
Astro.locals.pageCaching = false;
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
lang?: string;
|
lang?: string;
|
||||||
|
|
|
@ -10,6 +10,7 @@ import type { EndpointChronologyEvent } from "src/shared/payload/endpoint-types"
|
||||||
import { payload } from "src/services";
|
import { payload } from "src/services";
|
||||||
|
|
||||||
export const partial = true;
|
export const partial = true;
|
||||||
|
Astro.locals.pageCaching = false;
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
lang?: string;
|
lang?: string;
|
||||||
|
|
|
@ -18,6 +18,8 @@ import ChronologyEventPreview from "components/Previews/ChronologyEventPreview.a
|
||||||
import { Collections } from "src/shared/payload/constants";
|
import { Collections } from "src/shared/payload/constants";
|
||||||
import { meilisearch } from "src/services";
|
import { meilisearch } from "src/services";
|
||||||
|
|
||||||
|
Astro.locals.pageCaching = false;
|
||||||
|
|
||||||
type State = {
|
type State = {
|
||||||
q: string;
|
q: string;
|
||||||
page: number;
|
page: number;
|
||||||
|
|
Loading…
Reference in New Issue