Fixed revalidation

This commit is contained in:
DrMint 2023-04-30 11:50:44 +02:00
parent 0f6339c0f8
commit 6cc6635988
1 changed files with 80 additions and 76 deletions

View File

@ -13,7 +13,10 @@ type StrapiEvent = {
entry: Record<string, unknown>; entry: Record<string, unknown>;
}; };
type StrapiRelationalField = { count: number }; type StrapiRelationalFieldEntry = {
id: string;
slug: string;
};
type RequestProps = type RequestProps =
| CustomRequest | CustomRequest
@ -58,8 +61,8 @@ interface StrapiRangedContent extends StrapiEvent {
model: "ranged-content"; model: "ranged-content";
entry: { entry: {
id: string; id: string;
library_item: StrapiRelationalField; library_item?: StrapiRelationalFieldEntry;
content: StrapiRelationalField; content?: StrapiRelationalFieldEntry;
}; };
} }
@ -67,8 +70,8 @@ interface StrapiContent extends StrapiEvent {
model: "content"; model: "content";
entry: { entry: {
slug: string; slug: string;
folder: StrapiRelationalField; folder?: StrapiRelationalFieldEntry;
ranged_contents: StrapiRelationalField; ranged_contents: StrapiRelationalFieldEntry[];
}; };
} }
@ -109,7 +112,9 @@ interface StrapiLibraryItem extends StrapiEvent {
model: "library-item"; model: "library-item";
entry: { entry: {
slug: string; slug: string;
subitem_of: StrapiRelationalField; subitems: StrapiRelationalFieldEntry[];
subitem_of: StrapiRelationalFieldEntry[];
contents: StrapiRelationalFieldEntry[];
}; };
} }
@ -118,9 +123,9 @@ interface StrapiContentFolder extends StrapiEvent {
model: "contents-folder"; model: "contents-folder";
entry: { entry: {
slug: string; slug: string;
parent_folder: StrapiRelationalField; parent_folder?: StrapiRelationalFieldEntry;
subfolders: StrapiRelationalField; subfolders: StrapiRelationalFieldEntry[];
contents: StrapiRelationalField; contents: StrapiRelationalFieldEntry[];
}; };
} }
@ -149,7 +154,7 @@ interface StrapiChronicleChapter extends StrapiEvent {
event: CRUDEvents; event: CRUDEvents;
model: "chronicles-chapter"; model: "chronicles-chapter";
entry: { entry: {
chronicles: { slug: string }[]; chronicles: StrapiRelationalFieldEntry[];
}; };
} }
@ -158,7 +163,6 @@ interface StrapiVideo extends StrapiEvent {
model: "video"; model: "video";
entry: { entry: {
uid: string; uid: string;
channel: StrapiRelationalField;
}; };
} }
@ -182,7 +186,7 @@ const Revalidate = async (
const paths: string[] = []; const paths: string[] = [];
console.log(body); console.log(JSON.stringify(body, null, 2));
switch (body.model) { switch (body.model) {
case "post": { case "post": {
@ -194,15 +198,27 @@ const Revalidate = async (
paths.push(`/library/${body.entry.slug}`); paths.push(`/library/${body.entry.slug}`);
paths.push(`/library/${body.entry.slug}/reader`); paths.push(`/library/${body.entry.slug}/reader`);
if (body.entry.subitem_of.count > 0) { body.entry.subitem_of.map(({ slug: subItemOfSlug }) => {
const libraryItem = await sdk.getLibraryItem({ paths.push(`/library/${subItemOfSlug}`);
language_code: "en", }, []);
slug: body.entry.slug,
body.entry.subitems.map(({ slug: subItemSlug }) => {
paths.push(`/library/${subItemSlug}`);
}, []);
await Promise.all(
body.entry.contents.map(async ({ id }) => {
const rangedContent = await sdk.revalidationGetRangedContent({
id,
}); });
filterHasAttributes(libraryItem.libraryItems?.data[0]?.attributes?.subitem_of?.data, [
"attributes.slug", const contentSlug =
]).forEach((parentItem) => paths.push(`/library/${parentItem.attributes.slug}`)); rangedContent.rangedContent?.data?.attributes?.content?.data?.attributes?.slug;
if (contentSlug) {
paths.push(`/contents/${contentSlug}`);
} }
})
);
break; break;
} }
@ -210,29 +226,30 @@ const Revalidate = async (
case "content": { case "content": {
paths.push(`/contents/${body.entry.slug}`); paths.push(`/contents/${body.entry.slug}`);
if (body.entry.folder.count > 0 || body.entry.ranged_contents.count > 0) { const folderSlug = body.entry.folder?.slug;
const content = await sdk.getContentText({
language_code: "en",
slug: body.entry.slug,
});
const folderSlug = content.contents?.data[0]?.attributes?.folder?.data?.attributes?.slug;
if (folderSlug) { if (folderSlug) {
if (folderSlug === "root") { if (folderSlug === "root") {
paths.push(`/contents`); paths.push(`/contents`);
} else { } else {
paths.push(`/contents/folder/${folderSlug}`); paths.push(`/contents/folder/${body.entry.folder?.slug}`);
} }
} }
filterHasAttributes(content.contents?.data[0]?.attributes?.ranged_contents?.data, [ await Promise.all(
"attributes.library_item.data.attributes.slug", body.entry.ranged_contents.map(async ({ id }) => {
]).forEach((ranged_content) => { const rangedContent = await sdk.revalidationGetRangedContent({
const parentSlug = ranged_content.attributes.library_item.data.attributes.slug; id,
paths.push(`/library/${parentSlug}`);
paths.push(`/library/${parentSlug}/reader`);
}); });
const libraryItemSlug =
rangedContent.rangedContent?.data?.attributes?.library_item?.data?.attributes?.slug;
if (libraryItemSlug) {
paths.push(`/library/${libraryItemSlug}`);
paths.push(`/library/${libraryItemSlug}/reader`);
} }
})
);
break; break;
} }
@ -243,22 +260,14 @@ const Revalidate = async (
} }
case "ranged-content": { case "ranged-content": {
if (body.entry.content.count > 0 || body.entry.library_item.count > 0) { const libraryItemSlug = body.entry.library_item?.slug;
const rangedContent = await sdk.revalidationGetRangedContent({ const contentSlug = body.entry.content?.slug;
id: body.entry.id,
});
const libraryItemSlug =
rangedContent.rangedContent?.data?.attributes?.content?.data?.attributes?.slug;
if (libraryItemSlug) { if (libraryItemSlug) {
paths.push(`/library/${libraryItemSlug}`); paths.push(`/library/${libraryItemSlug}`);
paths.push(`/library/${libraryItemSlug}/reader`);
} }
const contentSlug =
rangedContent.rangedContent?.data?.attributes?.content?.data?.attributes?.slug;
if (contentSlug) { if (contentSlug) {
paths.push(`/contents/${contentSlug}`); paths.push(`/contents/${contentSlug}`);
} }
}
break; break;
} }
@ -269,27 +278,22 @@ const Revalidate = async (
paths.push(`/contents/folder/${body.entry.slug}`); paths.push(`/contents/folder/${body.entry.slug}`);
} }
if ( body.entry.contents.map(({ slug: contentSlug }) => {
body.entry.contents.count > 0 || paths.push(`/contents/${contentSlug}`);
body.entry.parent_folder.count > 0 ||
body.entry.subfolders.count > 0
) {
const folder = await sdk.getContentsFolder({
language_code: "en",
slug: body.entry.slug,
}); });
const parentSlug =
folder.contentsFolders?.data[0]?.attributes?.parent_folder?.data?.attributes?.slug; if (body.entry.parent_folder) {
if (parentSlug) { if (body.entry.parent_folder.slug === "root") {
paths.push(`/contents/folder/${parentSlug}`); paths.push(`/contents`);
} else {
paths.push(`/contents/folder/${body.entry.parent_folder.slug}`);
} }
filterHasAttributes(folder.contentsFolders?.data[0]?.attributes?.subfolders?.data, [
"attributes.slug",
]).forEach((subfolder) => paths.push(`/contents/folder/${subfolder.attributes.slug}`));
filterHasAttributes(folder.contentsFolders?.data[0]?.attributes?.contents?.data, [
"attributes.slug",
]).forEach((content) => paths.push(`/contents/${content.attributes.slug}`));
} }
body.entry.subfolders.map(({ slug: folderSlug }) => {
paths.push(`/contents/folder/${folderSlug}`);
});
break; break;
} }