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;
};
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<EndpointRecorder> =>
await request(getSDKEndpoint.getRecorderByIDEndpoint(id)),
getAllPaths: async (): Promise<EndpointAllPaths> =>
await request(getSDKEndpoint.getAllPathsEndpoint()),
getAllSdkUrls: async (): Promise<EndpointAllSDKUrls> =>
await request(getSDKEndpoint.getAllSDKUrlsEndpoint()),
request: async (pathname: string): Promise<any> => await request(pathname),
};
};

View File

@ -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");
}
};