diff --git a/src/components/AppLayout/components/Html.astro b/src/components/AppLayout/components/Html.astro index 9b2950d..7276f0d 100644 --- a/src/components/AppLayout/components/Html.astro +++ b/src/components/AppLayout/components/Html.astro @@ -1,7 +1,7 @@ --- import { UAParser } from "ua-parser-js"; import { getI18n } from "src/i18n/i18n"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; import { PostProcessingTags } from "src/middleware/postProcessing"; import type { EndpointAudio, diff --git a/src/components/AppLayout/components/Topbar/components/CurrencySelector.astro b/src/components/AppLayout/components/Topbar/components/CurrencySelector.astro index 58f0c31..bb5e0fc 100644 --- a/src/components/AppLayout/components/Topbar/components/CurrencySelector.astro +++ b/src/components/AppLayout/components/Topbar/components/CurrencySelector.astro @@ -2,7 +2,7 @@ import Button from "components/Button.astro"; import Tooltip from "components/Tooltip.astro"; import { getI18n } from "src/i18n/i18n"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; import { formatCurrency } from "src/utils/currencies"; import { PostProcessingTags, diff --git a/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro b/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro index 0daf10a..afa5a19 100644 --- a/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro +++ b/src/components/AppLayout/components/Topbar/components/LanguageSelector.astro @@ -2,7 +2,7 @@ import Button from "components/Button.astro"; import Tooltip from "components/Tooltip.astro"; import { getI18n } from "src/i18n/i18n"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; import { formatLocale } from "src/utils/format"; interface Props { diff --git a/src/i18n/i18n.ts b/src/i18n/i18n.ts index 92169a2..92aea73 100644 --- a/src/i18n/i18n.ts +++ b/src/i18n/i18n.ts @@ -1,5 +1,5 @@ import type { WordingKey } from "src/i18n/wordings-keys"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; import { capitalize, formatInlineTitle } from "src/utils/format"; import type { EndpointChronologyEvent, EndpointSource } from "src/shared/payload/endpoint-types"; diff --git a/src/middleware/pageCaching.ts b/src/middleware/pageCaching.ts index f9fc589..8a64230 100644 --- a/src/middleware/pageCaching.ts +++ b/src/middleware/pageCaching.ts @@ -1,5 +1,5 @@ import { defineMiddleware } from "astro:middleware"; -import { pageCache } from "src/utils/payload"; +import { pageCache } from "src/services"; export const pageCachingMiddleware = defineMiddleware(async ({ url, request, locals }, next) => { const pathname = url.pathname; diff --git a/src/middleware/utils.ts b/src/middleware/utils.ts index ffee7ec..67e48e1 100644 --- a/src/middleware/utils.ts +++ b/src/middleware/utils.ts @@ -1,6 +1,6 @@ import type { AstroCookies } from "astro"; import { z } from "astro:content"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; import acceptLanguage from "accept-language"; export const getAbsoluteLocaleUrl = (locale: string, url: string) => `/${locale}${url}`; diff --git a/src/pages/[locale]/_components/LibraryGrid.astro b/src/pages/[locale]/_components/LibraryGrid.astro index 26126d9..2e10156 100644 --- a/src/pages/[locale]/_components/LibraryGrid.astro +++ b/src/pages/[locale]/_components/LibraryGrid.astro @@ -1,7 +1,7 @@ --- import LibraryCard from "./LibraryCard.astro"; import { getI18n } from "src/i18n/i18n"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; const { getLocalizedUrl, getLocalizedMatch } = await getI18n(Astro.locals.currentLocale); --- diff --git a/src/pages/[locale]/api/hooks/collection-operation.ts b/src/pages/[locale]/api/hooks/collection-operation.ts index b6ce780..81d4469 100644 --- a/src/pages/[locale]/api/hooks/collection-operation.ts +++ b/src/pages/[locale]/api/hooks/collection-operation.ts @@ -1,7 +1,7 @@ import type { APIRoute } from "astro"; import { Collections } from "src/shared/payload/constants"; import type { AfterOperationWebHookMessage } from "src/shared/payload/webhooks"; -import { contextCache, dataCache } from "src/utils/payload.ts"; +import { contextCache, dataCache } from "src/services.ts"; export const POST: APIRoute = async ({ request }) => { const auth = request.headers.get("Authorization"); diff --git a/src/pages/[locale]/api/on-startup.ts b/src/pages/[locale]/api/on-startup.ts index 95c2f26..31facaf 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/services"; export const GET: APIRoute = async () => { await dataCache.init(); diff --git a/src/pages/[locale]/api/pages/partial.astro b/src/pages/[locale]/api/pages/partial.astro index d61f207..5be470e 100644 --- a/src/pages/[locale]/api/pages/partial.astro +++ b/src/pages/[locale]/api/pages/partial.astro @@ -7,7 +7,7 @@ import { getI18n } from "src/i18n/i18n"; import AsideLayout from "components/AppLayout/AsideLayout.astro"; import Attributes from "components/Attributes.astro"; import type { Attribute } from "src/utils/attributes"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { sizesToSrcset } from "src/utils/img"; import PageCredits from "./_components/PageCredits.astro"; import type { EndpointPage } from "src/shared/payload/endpoint-types"; diff --git a/src/pages/[locale]/api/timeline/partial.astro b/src/pages/[locale]/api/timeline/partial.astro index 4bf4019..7c167d3 100644 --- a/src/pages/[locale]/api/timeline/partial.astro +++ b/src/pages/[locale]/api/timeline/partial.astro @@ -7,7 +7,7 @@ import TimelineNote from "pages/[locale]/timeline/_components/TimelineNote.astro import TimelineSourcesButton from "pages/[locale]/timeline/_components/TimelineSourcesButton.astro"; import { getI18n } from "src/i18n/i18n"; import type { EndpointChronologyEvent } from "src/shared/payload/endpoint-types"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; export const partial = true; diff --git a/src/pages/[locale]/audios/[id].astro b/src/pages/[locale]/audios/[id].astro index 3172f0d..1aadf10 100644 --- a/src/pages/[locale]/audios/[id].astro +++ b/src/pages/[locale]/audios/[id].astro @@ -7,7 +7,7 @@ import Credits from "components/Credits.astro"; import DownloadButton from "components/DownloadButton.astro"; import RichText from "components/RichText/RichText.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; import { getFileIcon } from "src/utils/attributes"; diff --git a/src/pages/[locale]/collectibles/[slug]/gallery/[index].astro b/src/pages/[locale]/collectibles/[slug]/gallery/[index].astro index 94dd168..db4b47f 100644 --- a/src/pages/[locale]/collectibles/[slug]/gallery/[index].astro +++ b/src/pages/[locale]/collectibles/[slug]/gallery/[index].astro @@ -2,7 +2,7 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import Lightbox from "components/Lightbox.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; import { getFileIcon } from "src/utils/attributes"; diff --git a/src/pages/[locale]/collectibles/[slug]/gallery/index.astro b/src/pages/[locale]/collectibles/[slug]/gallery/index.astro index 727be69..3554428 100644 --- a/src/pages/[locale]/collectibles/[slug]/gallery/index.astro +++ b/src/pages/[locale]/collectibles/[slug]/gallery/index.astro @@ -2,7 +2,7 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; import { sizesToSrcset } from "src/utils/img"; diff --git a/src/pages/[locale]/collectibles/[slug]/index.astro b/src/pages/[locale]/collectibles/[slug]/index.astro index 18a4ff8..6739238 100644 --- a/src/pages/[locale]/collectibles/[slug]/index.astro +++ b/src/pages/[locale]/collectibles/[slug]/index.astro @@ -14,7 +14,7 @@ import { formatInlineTitle, formatLocale, formatRichTextToString } from "src/uti import AsideLayout from "components/AppLayout/AsideLayout.astro"; import Attributes from "components/Attributes.astro"; import type { Attribute } from "src/utils/attributes"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { sizesToSrcset } from "src/utils/img"; import RichText from "components/RichText/RichText.astro"; import SubFilesSection from "./_components/SubFilesSection.astro"; diff --git a/src/pages/[locale]/collectibles/[slug]/scans/[index].astro b/src/pages/[locale]/collectibles/[slug]/scans/[index].astro index a941c30..d31fb7d 100644 --- a/src/pages/[locale]/collectibles/[slug]/scans/[index].astro +++ b/src/pages/[locale]/collectibles/[slug]/scans/[index].astro @@ -2,7 +2,7 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import Lightbox from "components/Lightbox.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; diff --git a/src/pages/[locale]/collectibles/[slug]/scans/index.astro b/src/pages/[locale]/collectibles/[slug]/scans/index.astro index e961797..e42efed 100644 --- a/src/pages/[locale]/collectibles/[slug]/scans/index.astro +++ b/src/pages/[locale]/collectibles/[slug]/scans/index.astro @@ -3,7 +3,7 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro"; import Credits from "components/Credits.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { fetchOr404 } from "src/utils/responses"; import ScanPreview from "./_components/ScanPreview.astro"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; diff --git a/src/pages/[locale]/dev/trigger-500.astro b/src/pages/[locale]/dev/trigger-500.astro index 26f961b..1ee901c 100644 --- a/src/pages/[locale]/dev/trigger-500.astro +++ b/src/pages/[locale]/dev/trigger-500.astro @@ -1,6 +1,6 @@ --- import FiveOFive from "src/pages/500.astro"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; let error; diff --git a/src/pages/[locale]/files/[id].astro b/src/pages/[locale]/files/[id].astro index e9c22da..02e4761 100644 --- a/src/pages/[locale]/files/[id].astro +++ b/src/pages/[locale]/files/[id].astro @@ -6,7 +6,7 @@ import Credits from "components/Credits.astro"; import DownloadButton from "components/DownloadButton.astro"; import RichText from "components/RichText/RichText.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; import { getFileIcon } from "src/utils/attributes"; diff --git a/src/pages/[locale]/folders/[slug].astro b/src/pages/[locale]/folders/[slug].astro index d12b7bd..44b5a7d 100644 --- a/src/pages/[locale]/folders/[slug].astro +++ b/src/pages/[locale]/folders/[slug].astro @@ -11,7 +11,7 @@ import AudioPreview from "components/Previews/AudioPreview.astro"; import VideoPreview from "components/Previews/VideoPreview.astro"; import AppLayout from "components/AppLayout/AppLayout.astro"; import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import RichText from "components/RichText/RichText.astro"; import FilePreview from "components/Previews/FilePreview.astro"; import { Collections } from "src/shared/payload/constants"; diff --git a/src/pages/[locale]/images/[id].astro b/src/pages/[locale]/images/[id].astro index 731d241..7574f42 100644 --- a/src/pages/[locale]/images/[id].astro +++ b/src/pages/[locale]/images/[id].astro @@ -2,7 +2,7 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import Lightbox from "components/Lightbox.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; import { getFileIcon } from "src/utils/attributes"; diff --git a/src/pages/[locale]/index.astro b/src/pages/[locale]/index.astro index 6c83028..1debcce 100644 --- a/src/pages/[locale]/index.astro +++ b/src/pages/[locale]/index.astro @@ -3,7 +3,7 @@ import Button from "components/Button.astro"; import LibraryGrid from "./_components/LibraryGrid.astro"; import LinkCard from "./_components/LinkCard.astro"; import { getI18n } from "src/i18n/i18n"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; import AppLayout from "components/AppLayout/AppLayout.astro"; import HomeTitle from "./_components/HomeTitle.astro"; diff --git a/src/pages/[locale]/pages/[slug].astro b/src/pages/[locale]/pages/[slug].astro index d86dfb1..7f77558 100644 --- a/src/pages/[locale]/pages/[slug].astro +++ b/src/pages/[locale]/pages/[slug].astro @@ -2,7 +2,7 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import { getI18n } from "src/i18n/i18n"; import Page from "src/pages/[locale]/api/pages/partial.astro"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; diff --git a/src/pages/[locale]/recorders/[id].astro b/src/pages/[locale]/recorders/[id].astro index 1c9626f..d75159f 100644 --- a/src/pages/[locale]/recorders/[id].astro +++ b/src/pages/[locale]/recorders/[id].astro @@ -5,7 +5,7 @@ import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro import Attributes from "components/Attributes.astro"; import RichText from "components/RichText/RichText.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import type { Attribute } from "src/utils/attributes"; import { formatLocale } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; diff --git a/src/pages/[locale]/settings/index.astro b/src/pages/[locale]/settings/index.astro index 5453cbc..59e63f1 100644 --- a/src/pages/[locale]/settings/index.astro +++ b/src/pages/[locale]/settings/index.astro @@ -2,7 +2,7 @@ import AppLayout from "components/AppLayout/AppLayout.astro"; import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro"; import { getI18n } from "src/i18n/i18n"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; import { formatCurrency } from "src/utils/currencies"; import { formatLocale } from "src/utils/format"; import { prepareClassForSelectedCurrencyPostProcessing } from "src/middleware/postProcessing"; diff --git a/src/pages/[locale]/timeline/_components/TimelineYear.astro b/src/pages/[locale]/timeline/_components/TimelineYear.astro index 1d86fd3..5b531b9 100644 --- a/src/pages/[locale]/timeline/_components/TimelineYear.astro +++ b/src/pages/[locale]/timeline/_components/TimelineYear.astro @@ -2,7 +2,7 @@ import TimelineEvent from "./TimelineEvent.astro"; import { getI18n } from "src/i18n/i18n"; import type { EndpointChronologyEvent } from "src/shared/payload/endpoint-types"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; interface Props { year: number; diff --git a/src/pages/[locale]/timeline/index.astro b/src/pages/[locale]/timeline/index.astro index 5701b62..e1eef79 100644 --- a/src/pages/[locale]/timeline/index.astro +++ b/src/pages/[locale]/timeline/index.astro @@ -1,12 +1,12 @@ --- -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { groupBy } from "src/utils/array"; import TimelineYear from "./_components/TimelineYear.astro"; import AppLayout from "components/AppLayout/AppLayout.astro"; import AppLayoutTitle from "components/AppLayout/components/AppLayoutTitle.astro"; import Card from "components/Card.astro"; import { getI18n } from "src/i18n/i18n"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; import type { WordingKey } from "src/i18n/wordings-keys"; import { fetchOr404 } from "src/utils/responses"; diff --git a/src/pages/[locale]/videos/[id].astro b/src/pages/[locale]/videos/[id].astro index 6cdb6e7..a12d62d 100644 --- a/src/pages/[locale]/videos/[id].astro +++ b/src/pages/[locale]/videos/[id].astro @@ -7,7 +7,7 @@ import DownloadButton from "components/DownloadButton.astro"; import RichText from "components/RichText/RichText.astro"; import VideoPlayer from "components/VideoPlayer.astro"; import { getI18n } from "src/i18n/i18n"; -import { payload } from "src/utils/payload"; +import { payload } from "src/services"; import { formatInlineTitle, formatRichTextToString } from "src/utils/format"; import { fetchOr404 } from "src/utils/responses"; import { getFileIcon } from "src/utils/attributes"; diff --git a/src/services.ts b/src/services.ts index 7bd7248..5345fd6 100644 --- a/src/services.ts +++ b/src/services.ts @@ -1,5 +1,10 @@ import { AnalyticsSDK } from "src/shared/analytics/sdk"; import { MeilisearchSDK } from "src/shared/meilisearch/sdk"; +import { ContextCache } from "src/cache/contextCache"; +import { DataCache } from "src/cache/dataCache"; +import { PageCache } from "src/cache/pageCache"; +import { TokenCache } from "src/cache/tokenCache"; +import { PayloadSDK } from "src/shared/payload/sdk"; export const meilisearch = new MeilisearchSDK( import.meta.env.MEILISEARCH_URL, @@ -7,3 +12,29 @@ export const meilisearch = new MeilisearchSDK( ); export const analytics = new AnalyticsSDK(import.meta.env.ANALYTICS_URL); + +const tokenCache = new TokenCache(); + +export const payload = new PayloadSDK( + import.meta.env.PAYLOAD_API_URL, + import.meta.env.PAYLOAD_USER, + import.meta.env.PAYLOAD_PASSWORD +); +payload.addTokenCache(tokenCache); + +const uncachedPayload = new PayloadSDK( + import.meta.env.PAYLOAD_API_URL, + import.meta.env.PAYLOAD_USER, + import.meta.env.PAYLOAD_PASSWORD +); +uncachedPayload.addTokenCache(tokenCache); +export const pageCache = new PageCache(uncachedPayload); + +// Loading context cache first so that the server can still serve responses while precaching. +export const contextCache = new ContextCache(payload); +await contextCache.init(); + +export const dataCache = new DataCache(payload, uncachedPayload, (urls) => + pageCache.invalidate(urls) +); +payload.addDataCache(dataCache); diff --git a/src/utils/format.ts b/src/utils/format.ts index b5aebc6..b8cc4f3 100644 --- a/src/utils/format.ts +++ b/src/utils/format.ts @@ -7,7 +7,7 @@ import { type RichTextContent, type RichTextNode, } from "src/shared/payload/rich-text"; -import { contextCache } from "src/utils/payload"; +import { contextCache } from "src/services"; export const formatLocale = (code: string): string => contextCache.languages.find(({ id }) => id === code)?.name ?? code; diff --git a/src/utils/payload.ts b/src/utils/payload.ts deleted file mode 100644 index ce36b8f..0000000 --- a/src/utils/payload.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { ContextCache } from "src/cache/contextCache"; -import { DataCache } from "src/cache/dataCache"; -import { PageCache } from "src/cache/pageCache"; -import { TokenCache } from "src/cache/tokenCache"; -import { PayloadSDK } from "src/shared/payload/sdk"; - -const tokenCache = new TokenCache(); - -const payload = new PayloadSDK( - import.meta.env.PAYLOAD_API_URL, - import.meta.env.PAYLOAD_USER, - import.meta.env.PAYLOAD_PASSWORD -); -payload.addTokenCache(tokenCache); - -const uncachedPayload = new PayloadSDK( - import.meta.env.PAYLOAD_API_URL, - import.meta.env.PAYLOAD_USER, - import.meta.env.PAYLOAD_PASSWORD -); -uncachedPayload.addTokenCache(tokenCache); -const pageCache = new PageCache(uncachedPayload); - -// Loading context cache first so that the server can still serve responses while precaching. -const contextCache = new ContextCache(payload); -await contextCache.init(); - -const dataCache = new DataCache(payload, uncachedPayload, (urls) => pageCache.invalidate(urls)); -payload.addDataCache(dataCache); - -export { payload, contextCache, pageCache, dataCache };