Improved and simplify precaching

This commit is contained in:
DrMint 2024-06-26 06:43:19 +02:00
parent e89a125052
commit d9ef48d811
2 changed files with 11 additions and 88 deletions

View File

@ -2076,16 +2076,8 @@ export type PayloadImage = PayloadMedia & {
height: number; height: number;
}; };
export type EndpointAllPaths = { export type EndpointAllSDKUrls = {
collectibles: string[]; urls: string[];
pages: string[];
folders: string[];
videos: string[];
audios: string[];
images: string[];
files: string[];
recorders: string[];
chronologyEvents: string[];
}; };
// SDK // SDK
@ -2128,7 +2120,7 @@ export const getSDKEndpoint = {
getVideoByIDEndpoint: (id: string) => `/${Collections.Videos}/id/${id}`, getVideoByIDEndpoint: (id: string) => `/${Collections.Videos}/id/${id}`,
getFileByIDEndpoint: (id: string) => `/${Collections.Files}/id/${id}`, getFileByIDEndpoint: (id: string) => `/${Collections.Files}/id/${id}`,
getRecorderByIDEndpoint: (id: string) => `/${Collections.Recorders}/id/${id}`, getRecorderByIDEndpoint: (id: string) => `/${Collections.Recorders}/id/${id}`,
getAllPathsEndpoint: () => `/all-paths`, getAllSDKUrlsEndpoint: () => `/all-sdk-urls`,
getLoginEndpoint: () => `/${Collections.Recorders}/login`, getLoginEndpoint: () => `/${Collections.Recorders}/login`,
}; };
@ -2225,8 +2217,8 @@ export const getPayloadSDK = ({
await request(getSDKEndpoint.getFileByIDEndpoint(id)), await request(getSDKEndpoint.getFileByIDEndpoint(id)),
getRecorderByID: async (id: string): Promise<EndpointRecorder> => getRecorderByID: async (id: string): Promise<EndpointRecorder> =>
await request(getSDKEndpoint.getRecorderByIDEndpoint(id)), await request(getSDKEndpoint.getRecorderByIDEndpoint(id)),
getAllPaths: async (): Promise<EndpointAllPaths> => getAllSdkUrls: async (): Promise<EndpointAllSDKUrls> =>
await request(getSDKEndpoint.getAllPathsEndpoint()), await request(getSDKEndpoint.getAllSDKUrlsEndpoint()),
request: async (pathname: string): Promise<any> => await request(pathname), request: async (pathname: string): Promise<any> => await request(pathname),
}; };
}; };

View File

@ -125,85 +125,16 @@ export const initPayload = async () => {
return; return;
} }
const result = await payload.getAllPaths(); const { urls } = await payload.getAllSdkUrls();
for (const url of urls) {
for (const slug of result.pages) {
try { try {
await payload.getPage(slug); await payload.request(url);
} catch (e) { } catch {
console.warn("[Precaching] Couldn't precache page", slug, e); console.warn("[ResponseCaching] Precaching failed for url", url);
} }
} }
console.log("[ResponseCaching] Precaching completed!", responseCache.size, "responses cached");
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);
}
payloadInitialized = true; payloadInitialized = true;
console.log("[ResponseCaching] Precaching completed!", responseCache.size, "responses cached");
} }
}; };