Only use selectable languages
This commit is contained in:
parent
2a505ebd7a
commit
efb0f03be8
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
|
|
|
@ -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<string>(urls);
|
||||
|
||||
ids.forEach((id) => {
|
||||
|
|
|
@ -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<string>();
|
||||
|
||||
sdkUrls.forEach((url) => {
|
||||
|
|
|
@ -21,11 +21,11 @@ const { t } = await getI18n(currentLocale);
|
|||
<Tooltip trigger="click" {...otherProps.class ? otherProps : {}}>
|
||||
<div id="content" slot="tooltip-content">
|
||||
{
|
||||
contextCache.locales.map(({ id }) => (
|
||||
contextCache.locales.map((locale) => (
|
||||
<a
|
||||
class:list={{ current: currentLocale === id, "pressable-link": true }}
|
||||
href={`?action-lang=${id}`}>
|
||||
{formatLocale(id)}
|
||||
class:list={{ current: currentLocale === locale, "pressable-link": true }}
|
||||
href={`?action-lang=${locale}`}>
|
||||
{formatLocale(locale)}
|
||||
</a>
|
||||
))
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
});
|
||||
|
|
|
@ -17,8 +17,8 @@ export const redirect = (redirectURL: string, headers: Record<string, string> =
|
|||
|
||||
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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -21,11 +21,11 @@ const { t } = await getI18n(currentLocale);
|
|||
<h2>{t("settings.language.title")}</h2>
|
||||
<p>{t("settings.language.description")}</p><br />
|
||||
{
|
||||
contextCache.locales.map(({ id }) => (
|
||||
contextCache.locales.map((locale) => (
|
||||
<a
|
||||
class:list={{ current: currentLocale === id, "pressable-link": true }}
|
||||
href={`?action-lang=${id}`}>
|
||||
{formatLocale(id)}
|
||||
class:list={{ current: currentLocale === locale, "pressable-link": true }}
|
||||
href={`?action-lang=${locale}`}>
|
||||
{formatLocale(locale)}
|
||||
</a>
|
||||
))
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue