From d9ef48d811e92192e68b9b26708108f316c0d474 Mon Sep 17 00:00:00 2001 From: DrMint <29893320+DrMint@users.noreply.github.com> Date: Wed, 26 Jun 2024 06:43:19 +0200 Subject: [PATCH] Improved and simplify precaching --- src/shared/payload/payload-sdk.ts | 18 ++----- src/utils/payload.ts | 81 +++---------------------------- 2 files changed, 11 insertions(+), 88 deletions(-) diff --git a/src/shared/payload/payload-sdk.ts b/src/shared/payload/payload-sdk.ts index a6f5bea..92672bc 100644 --- a/src/shared/payload/payload-sdk.ts +++ b/src/shared/payload/payload-sdk.ts @@ -2076,16 +2076,8 @@ export type PayloadImage = PayloadMedia & { height: number; }; -export type EndpointAllPaths = { - collectibles: string[]; - pages: string[]; - folders: string[]; - videos: string[]; - audios: string[]; - images: string[]; - files: string[]; - recorders: string[]; - chronologyEvents: string[]; +export type EndpointAllSDKUrls = { + urls: string[]; }; // SDK @@ -2128,7 +2120,7 @@ export const getSDKEndpoint = { getVideoByIDEndpoint: (id: string) => `/${Collections.Videos}/id/${id}`, getFileByIDEndpoint: (id: string) => `/${Collections.Files}/id/${id}`, getRecorderByIDEndpoint: (id: string) => `/${Collections.Recorders}/id/${id}`, - getAllPathsEndpoint: () => `/all-paths`, + getAllSDKUrlsEndpoint: () => `/all-sdk-urls`, getLoginEndpoint: () => `/${Collections.Recorders}/login`, }; @@ -2225,8 +2217,8 @@ export const getPayloadSDK = ({ await request(getSDKEndpoint.getFileByIDEndpoint(id)), getRecorderByID: async (id: string): Promise => await request(getSDKEndpoint.getRecorderByIDEndpoint(id)), - getAllPaths: async (): Promise => - await request(getSDKEndpoint.getAllPathsEndpoint()), + getAllSdkUrls: async (): Promise => + await request(getSDKEndpoint.getAllSDKUrlsEndpoint()), request: async (pathname: string): Promise => await request(pathname), }; }; diff --git a/src/utils/payload.ts b/src/utils/payload.ts index e290160..7f075bd 100644 --- a/src/utils/payload.ts +++ b/src/utils/payload.ts @@ -125,85 +125,16 @@ export const initPayload = async () => { return; } - const result = await payload.getAllPaths(); - - for (const slug of result.pages) { + const { urls } = await payload.getAllSdkUrls(); + for (const url of urls) { try { - await payload.getPage(slug); - } catch (e) { - console.warn("[Precaching] Couldn't precache page", slug, e); + await payload.request(url); + } catch { + console.warn("[ResponseCaching] Precaching failed for url", url); } } - - for (const slug of result.folders) { - try { - await payload.getFolder(slug); - } catch (e) { - console.warn("[Precaching] Couldn't precache folder", slug, e); - } - } - - for (const slug of result.collectibles) { - try { - const collectible = await payload.getCollectible(slug); - if (collectible.scans) { - await payload.getCollectibleScans(slug); - } - if (collectible.gallery) { - await payload.getCollectibleGallery(slug); - } - } catch (e) { - console.warn("[Precaching] Couldn't precache collectible", slug, e); - } - } - - for (const id of result.recorders) { - try { - await payload.getRecorderByID(id); - } catch (e) { - console.warn("[Precaching] Couldn't precache recorder", id, e); - } - } - - for (const id of result.audios) { - try { - await payload.getAudioByID(id); - } catch (e) { - console.warn("[Precaching] Couldn't precache audio", id, e); - } - } - - for (const id of result.videos) { - try { - await payload.getVideoByID(id); - } catch (e) { - console.warn("[Precaching] Couldn't precache video", id, e); - } - } - - for (const id of result.images) { - try { - await payload.getImageByID(id); - } catch (e) { - console.warn("[Precaching] Couldn't precache image", id, e); - } - } - - for (const id of result.files) { - try { - await payload.getFileByID(id); - } catch (e) { - console.warn("[Precaching] Couldn't precache file", id, e); - } - } - - try { - await payload.getChronologyEvents(); - } catch (e) { - console.warn("[Precaching] Couldn't precache chronology events", e); - } + console.log("[ResponseCaching] Precaching completed!", responseCache.size, "responses cached"); payloadInitialized = true; - console.log("[ResponseCaching] Precaching completed!", responseCache.size, "responses cached"); } };