diff --git a/.env.example b/.env.example index be79076..f2ee9df 100644 --- a/.env.example +++ b/.env.example @@ -8,7 +8,11 @@ PAYLOAD_USER=myemail@domain.com PAYLOAD_PASSWORD=somepassword123 WEB_HOOK_TOKEN=webhookd5e6ea45ef4e66eaa151612bdcb599df -ENABLE_PRECACHING=true +## CACHING +DATA_CACHING=false +DATA_PRECACHING=false +PAGE_CACHING=false +PAGE_PRECACHING=false ## OPEN EXCHANGE RATE OER_APP_ID=oerappid5e6ea45ef4e66eaa151612bdcb599df diff --git a/src/cache/contextCache.ts b/src/cache/contextCache.ts index c74d861..5ee83ab 100644 --- a/src/cache/contextCache.ts +++ b/src/cache/contextCache.ts @@ -12,7 +12,8 @@ export class ContextCache { constructor(private readonly payload: PayloadSDK) {} - locales: Language[] = []; + languages: Language[] = []; + locales: string[] = []; currencies: string[] = []; wordings: EndpointWording[] = []; config: EndpointWebsiteConfig = { @@ -45,7 +46,8 @@ export class ContextCache { } async refreshLocales() { - this.locales = (await this.payload.getLanguages()).data; + this.languages = (await this.payload.getLanguages()).data; + this.locales = this.languages.filter(({ selectable }) => selectable).map(({ id }) => id); this.logger.log("Locales refreshed"); } diff --git a/src/cache/dataCache.ts b/src/cache/dataCache.ts index da6d8c2..5547311 100644 --- a/src/cache/dataCache.ts +++ b/src/cache/dataCache.ts @@ -24,7 +24,7 @@ export class DataCache { async init() { if (this.initialized) return; - if (import.meta.env.ENABLE_PRECACHING === "true") { + if (import.meta.env.DATA_PRECACHING === "true") { await this.precache(); } @@ -67,6 +67,7 @@ export class DataCache { } get(url: string) { + if (import.meta.env.DATA_CACHING !== "true") return; const cachedResponse = this.responseCache.get(url); if (cachedResponse) { this.logger.log("Retrieved cached response for", url); @@ -75,6 +76,7 @@ export class DataCache { } set(url: string, response: any) { + if (import.meta.env.DATA_CACHING !== "true") return; const stringData = JSON.stringify(response); const regex = /[a-f0-9]{24}/g; const ids = [...stringData.matchAll(regex)].map((match) => match[0]); @@ -97,6 +99,7 @@ export class DataCache { } async invalidate(ids: string[], urls: string[]) { + if (import.meta.env.DATA_CACHING !== "true") return; const urlsToInvalidate = new Set(urls); ids.forEach((id) => { diff --git a/src/cache/pageCache.ts b/src/cache/pageCache.ts index 409936e..79890f2 100644 --- a/src/cache/pageCache.ts +++ b/src/cache/pageCache.ts @@ -26,16 +26,17 @@ export class PageCache { async init() { if (this.initialized) return; - if (import.meta.env.ENABLE_PRECACHING === "true") { - await this.precacheAll(); + if (import.meta.env.PAGE_PRECACHING === "true") { + await this.precache(); } this.initialized = true; } - private async precacheAll() { + private async precache() { + if (import.meta.env.DATA_CACHING !== "true") return; const { data: languages } = await this.uncachedPayload.getLanguages(); - const locales = languages.map(({ id }) => id); + const locales = languages.filter(({ selectable }) => selectable).map(({ id }) => id); // Get all pages urls from CMS const allPagesUrls = [ @@ -99,6 +100,7 @@ export class PageCache { } get(url: string): Response | undefined { + if (import.meta.env.PAGE_CACHING !== "true") return; const cachedPage = this.responseCache.get(url); if (cachedPage) { this.logger.log("Retrieved cached page for", url); @@ -108,6 +110,7 @@ export class PageCache { } set(url: string, response: Response, sdkCalls: string[]) { + if (import.meta.env.PAGE_CACHING !== "true") return; sdkCalls.forEach((id) => { const current = this.invalidationMap.get(id); if (current) { @@ -125,6 +128,7 @@ export class PageCache { } async invalidate(sdkUrls: string[]) { + if (import.meta.env.PAGE_CACHING !== "true") return; const pagesToInvalidate = new Set(); sdkUrls.forEach((url) => { diff --git a/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro b/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro index 3545e5a..f072bea 100644 --- a/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro +++ b/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro @@ -21,11 +21,11 @@ const { t } = await getI18n(currentLocale);
{ - contextCache.locales.map(({ id }) => ( + contextCache.locales.map((locale) => ( - {formatLocale(id)} + class:list={{ current: currentLocale === locale, "pressable-link": true }} + href={`?action-lang=${locale}`}> + {formatLocale(locale)} )) } diff --git a/src/middleware/commonHeaders.ts b/src/middleware/commonHeaders.ts index eaf70a8..849733d 100644 --- a/src/middleware/commonHeaders.ts +++ b/src/middleware/commonHeaders.ts @@ -12,7 +12,7 @@ export const addCommonHeadersMiddleware = defineMiddleware(async ({ url }, next) // TODO: Remove when in production response.headers.set("X-Robots-Tag", "none"); response.headers.set("Vary", "Cookie"); - response.headers.set("Cache-Control", "max-age=3600, stale-while-revalidate=3600"); + response.headers.set("Cache-Control", "max-age=86400, stale-while-revalidate=86400"); return response; }); diff --git a/src/middleware/utils.ts b/src/middleware/utils.ts index e7bf44f..ffee7ec 100644 --- a/src/middleware/utils.ts +++ b/src/middleware/utils.ts @@ -17,8 +17,8 @@ export const redirect = (redirectURL: string, headers: Record = export const getCurrentLocale = (pathname: string): string | undefined => { for (const locale of contextCache.locales) { - if (pathname.split("/")[1] === locale.id) { - return locale.id; + if (pathname.split("/")[1] === locale) { + return locale; } } return undefined; @@ -28,7 +28,7 @@ export const getBestAcceptedLanguage = (request: Request): string | undefined => const header = request.headers.get("Accept-Language"); if (!header || header === "*") return; - acceptLanguage.languages(contextCache.locales.map(({ id }) => id)); + acceptLanguage.languages(contextCache.locales); return acceptLanguage.get(request.headers.get("Accept-Language")) ?? undefined; }; @@ -62,9 +62,7 @@ export const isValidCurrency = (currency: string | null | undefined): currency i currency !== null && currency != undefined && contextCache.currencies.includes(currency); export const isValidLocale = (locale: string | null | undefined): locale is string => - locale !== null && - locale != undefined && - contextCache.locales.map(({ id }) => id).includes(locale); + locale !== null && locale != undefined && contextCache.locales.includes(locale); export const isValidTheme = ( theme: string | null | undefined diff --git a/src/pages/[locale]/api/on-startup.ts b/src/pages/[locale]/api/on-startup.ts index ba6eb85..95c2f26 100644 --- a/src/pages/[locale]/api/on-startup.ts +++ b/src/pages/[locale]/api/on-startup.ts @@ -1,5 +1,5 @@ import type { APIRoute } from "astro"; -import { dataCache, pageCache } from "src/utils/payload"; +import { dataCache, pageCache } from "src/utils/payload"; export const GET: APIRoute = async () => { await dataCache.init(); diff --git a/src/pages/[locale]/settings/index.astro b/src/pages/[locale]/settings/index.astro index 141a201..5453cbc 100644 --- a/src/pages/[locale]/settings/index.astro +++ b/src/pages/[locale]/settings/index.astro @@ -21,11 +21,11 @@ const { t } = await getI18n(currentLocale);

{t("settings.language.title")}

{t("settings.language.description")}


{ - contextCache.locales.map(({ id }) => ( + contextCache.locales.map((locale) => ( - {formatLocale(id)} + class:list={{ current: currentLocale === locale, "pressable-link": true }} + href={`?action-lang=${locale}`}> + {formatLocale(locale)} )) } diff --git a/src/shared/analytics/analytics.ts b/src/shared/analytics/analytics.ts index 9dac268..c0d216d 100644 --- a/src/shared/analytics/analytics.ts +++ b/src/shared/analytics/analytics.ts @@ -2,8 +2,8 @@ import { contextCache } from "src/utils/payload"; const getUnlocalizedPathname = (pathname: string): string => { for (const locale of contextCache.locales) { - if (pathname.startsWith(`/${locale.id}`)) { - return pathname.substring(`/${locale.id}`.length) || "/"; + if (pathname.startsWith(`/${locale}`)) { + return pathname.substring(`/${locale}`.length) || "/"; } } return pathname; diff --git a/src/shared/payload/payload-sdk.ts b/src/shared/payload/payload-sdk.ts index 1e76ba3..50b66f3 100644 --- a/src/shared/payload/payload-sdk.ts +++ b/src/shared/payload/payload-sdk.ts @@ -227,6 +227,7 @@ export interface Image { export interface Language { id: string; name: string; + selectable: boolean; } /** * This interface was referenced by `Config`'s JSON-Schema diff --git a/src/utils/format.ts b/src/utils/format.ts index 9ed396f..47282a2 100644 --- a/src/utils/format.ts +++ b/src/utils/format.ts @@ -9,7 +9,7 @@ import { import { contextCache } from "src/utils/payload"; export const formatLocale = (code: string): string => - contextCache.locales.find(({ id }) => id === code)?.name ?? code; + contextCache.languages.find(({ id }) => id === code)?.name ?? code; export const formatInlineTitle = ({ pretitle,