From e26ac32903c4490ac03a29a934614dc8674b9fab Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Mon, 29 Jul 2024 07:57:36 +0200 Subject: [PATCH] Disable client side page cache on some pages --- src/middleware/commonHeaders.ts | 10 ++++++---- src/pages/404.astro | 2 ++ src/pages/500.astro | 2 ++ src/pages/[locale]/discord.astro | 1 + src/pages/[locale]/search/index.astro | 1 + 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/middleware/commonHeaders.ts b/src/middleware/commonHeaders.ts index 3ae251d..2581a57 100644 --- a/src/middleware/commonHeaders.ts +++ b/src/middleware/commonHeaders.ts @@ -13,10 +13,12 @@ export const addCommonHeadersMiddleware = defineMiddleware(async ({ url }, next) response.headers.set("X-Robots-Tag", "none"); response.headers.set("Vary", "Cookie"); - if (import.meta.env.CACHE_CONTROL !== "true") { - response.headers.set("Cache-Control", "no-store"); - } else { - response.headers.set("Cache-Control", "max-age=86400, stale-while-revalidate=86400"); + if (!response.headers.has("cache-control")) { + if (import.meta.env.CACHE_CONTROL !== "true" && !response.headers.has("cache-control")) { + response.headers.set("Cache-Control", "no-store"); + } else { + response.headers.set("Cache-Control", "max-age=86400, stale-while-revalidate=86400"); + } } return response; diff --git a/src/pages/404.astro b/src/pages/404.astro index cbe8e58..0f20909 100644 --- a/src/pages/404.astro +++ b/src/pages/404.astro @@ -3,6 +3,8 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import Button from "components/Button.astro"; import { getI18n } from "src/i18n/i18n"; +Astro.response.headers.set("Cache-Control", "no-store"); + const { t, getLocalizedUrl } = await getI18n(Astro.locals.currentLocale); --- diff --git a/src/pages/500.astro b/src/pages/500.astro index e2fe1f8..329badd 100644 --- a/src/pages/500.astro +++ b/src/pages/500.astro @@ -4,6 +4,8 @@ import Button from "components/Button.astro"; import Card from "components/Card.astro"; import { getI18n } from "src/i18n/i18n"; +Astro.response.headers.set("Cache-Control", "no-store"); + interface Props { error: unknown; } diff --git a/src/pages/[locale]/discord.astro b/src/pages/[locale]/discord.astro index a97e514..bf911f3 100644 --- a/src/pages/[locale]/discord.astro +++ b/src/pages/[locale]/discord.astro @@ -1,3 +1,4 @@ --- +Astro.response.headers.set("Cache-Control", "no-store"); return Astro.redirect("https://discord.com/invite/5mcXcRAczj"); --- diff --git a/src/pages/[locale]/search/index.astro b/src/pages/[locale]/search/index.astro index 38bac45..c93d0ba 100644 --- a/src/pages/[locale]/search/index.astro +++ b/src/pages/[locale]/search/index.astro @@ -19,6 +19,7 @@ import { Collections } from "src/shared/payload/constants"; import { meilisearch } from "src/services"; Astro.locals.pageCaching = false; +Astro.response.headers.set("Cache-Control", "no-store"); type State = { q: string;