Improved revalidation api
This commit is contained in:
parent
8862be4118
commit
7db5578b3c
|
@ -102,3 +102,9 @@ export const arrayMove = <T>(arr: T[], sourceIndex: number, targetIndex: number)
|
||||||
arr.splice(targetIndex, 0, arr.splice(sourceIndex, 1)[0]);
|
arr.splice(targetIndex, 0, arr.splice(sourceIndex, 1)[0]);
|
||||||
return arr;
|
return arr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const cartesianProduct = <T, U>(arrayA: T[], arrayB: U[]): [T, U][] => {
|
||||||
|
const result: [T, U][] = [];
|
||||||
|
arrayA.forEach((a) => arrayB.forEach((b) => result.push([a, b])));
|
||||||
|
return result;
|
||||||
|
};
|
|
@ -1,20 +1,34 @@
|
||||||
import type { NextApiRequest, NextApiResponse } from "next";
|
import type { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { i18n } from "../../../next.config";
|
import { i18n } from "../../../next.config";
|
||||||
|
import { cartesianProduct } from "helpers/others";
|
||||||
|
|
||||||
|
type CRUDEvents = "entry.create" | "entry.delete" | "entry.update";
|
||||||
|
|
||||||
|
type StrapiEvent = {
|
||||||
|
event: CRUDEvents;
|
||||||
|
model: string;
|
||||||
|
entry: Record<string, unknown>;
|
||||||
|
};
|
||||||
|
|
||||||
type RequestProps =
|
type RequestProps =
|
||||||
| HookChronicle
|
| CustomRequest
|
||||||
| HookChronicleChapter
|
| StrapiChronicle
|
||||||
| HookChronology
|
| StrapiChronicleChapter
|
||||||
| HookContent
|
| StrapiChronology
|
||||||
| HookContentFolder
|
| StrapiContent
|
||||||
| HookCustom
|
| StrapiContentFolder
|
||||||
| HookLibraryItem
|
| StrapiLibraryItem
|
||||||
| HookPostContent
|
| StrapiPostContent
|
||||||
| HookRangedContent
|
| StrapiRangedContent
|
||||||
| HookWiki;
|
| StrapiWiki;
|
||||||
|
|
||||||
type HookRangedContent = {
|
interface CustomRequest {
|
||||||
event: "entry.create" | "entry.delete" | "entry.update";
|
model: "custom";
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface StrapiRangedContent extends StrapiEvent {
|
||||||
|
event: CRUDEvents;
|
||||||
model: "ranged-content";
|
model: "ranged-content";
|
||||||
entry: {
|
entry: {
|
||||||
library_item?: {
|
library_item?: {
|
||||||
|
@ -24,14 +38,9 @@ type HookRangedContent = {
|
||||||
slug: string;
|
slug: string;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type HookCustom = {
|
interface StrapiContent extends StrapiEvent {
|
||||||
model: "custom";
|
|
||||||
url: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type HookContent = {
|
|
||||||
model: "content";
|
model: "content";
|
||||||
entry: {
|
entry: {
|
||||||
slug: string;
|
slug: string;
|
||||||
|
@ -42,18 +51,18 @@ type HookContent = {
|
||||||
slug: string;
|
slug: string;
|
||||||
}[];
|
}[];
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type HookPostContent = {
|
interface StrapiPostContent extends StrapiEvent {
|
||||||
event: "entry.create" | "entry.delete" | "entry.update";
|
event: CRUDEvents;
|
||||||
model: "post";
|
model: "post";
|
||||||
entry: {
|
entry: {
|
||||||
slug: string;
|
slug: string;
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type HookLibraryItem = {
|
interface StrapiLibraryItem extends StrapiEvent {
|
||||||
event: "entry.create" | "entry.delete" | "entry.update";
|
event: CRUDEvents;
|
||||||
model: "library-item";
|
model: "library-item";
|
||||||
entry: {
|
entry: {
|
||||||
slug: string;
|
slug: string;
|
||||||
|
@ -63,10 +72,10 @@ type HookLibraryItem = {
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type HookContentFolder = {
|
interface StrapiContentFolder extends StrapiEvent {
|
||||||
event: "entry.create" | "entry.delete" | "entry.update";
|
event: CRUDEvents;
|
||||||
model: "contents-folder";
|
model: "contents-folder";
|
||||||
entry: {
|
entry: {
|
||||||
slug: string;
|
slug: string;
|
||||||
|
@ -78,36 +87,36 @@ type HookContentFolder = {
|
||||||
slug: string;
|
slug: string;
|
||||||
}[];
|
}[];
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type HookChronology = {
|
interface StrapiChronology extends StrapiEvent {
|
||||||
event: "entry.create" | "entry.delete" | "entry.update";
|
event: CRUDEvents;
|
||||||
model: "chronology-era" | "chronology-item";
|
model: "chronology-era" | "chronology-item";
|
||||||
};
|
}
|
||||||
|
|
||||||
type HookWiki = {
|
interface StrapiWiki extends StrapiEvent {
|
||||||
event: "entry.create" | "entry.delete" | "entry.update";
|
event: CRUDEvents;
|
||||||
model: "wiki-page";
|
model: "wiki-page";
|
||||||
entry: {
|
entry: {
|
||||||
slug: string;
|
slug: string;
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type HookChronicle = {
|
interface StrapiChronicle extends StrapiEvent {
|
||||||
event: "entry.create" | "entry.delete" | "entry.update";
|
event: CRUDEvents;
|
||||||
model: "chronicle";
|
model: "chronicle";
|
||||||
entry: {
|
entry: {
|
||||||
slug: string;
|
slug: string;
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type HookChronicleChapter = {
|
interface StrapiChronicleChapter extends StrapiEvent {
|
||||||
event: "entry.create" | "entry.delete" | "entry.update";
|
event: CRUDEvents;
|
||||||
model: "chronicles-chapter";
|
model: "chronicles-chapter";
|
||||||
entry: {
|
entry: {
|
||||||
chronicles: { slug: string }[];
|
chronicles: { slug: string }[];
|
||||||
};
|
};
|
||||||
};
|
}
|
||||||
|
|
||||||
type ResponseMailProps = {
|
type ResponseMailProps = {
|
||||||
message: string;
|
message: string;
|
||||||
|
@ -129,10 +138,6 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
|
||||||
case "post": {
|
case "post": {
|
||||||
paths.push(`/news`);
|
paths.push(`/news`);
|
||||||
paths.push(`/news/${body.entry.slug}`);
|
paths.push(`/news/${body.entry.slug}`);
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/news`);
|
|
||||||
paths.push(`/${locale}/news/${body.entry.slug}`);
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,14 +148,6 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
|
||||||
body.entry.subitem_of.forEach((parentItem) => {
|
body.entry.subitem_of.forEach((parentItem) => {
|
||||||
paths.push(`/library/${parentItem.slug}`);
|
paths.push(`/library/${parentItem.slug}`);
|
||||||
});
|
});
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/library`);
|
|
||||||
paths.push(`/${locale}/library/${body.entry.slug}`);
|
|
||||||
paths.push(`/${locale}/library/${body.entry.slug}/scans`);
|
|
||||||
body.entry.subitem_of.forEach((parentItem) => {
|
|
||||||
paths.push(`/${locale}/library/${parentItem.slug}`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,13 +158,6 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
|
||||||
if (body.entry.folder?.slug) {
|
if (body.entry.folder?.slug) {
|
||||||
paths.push(`/contents/folder/${body.entry.folder.slug}`);
|
paths.push(`/contents/folder/${body.entry.folder.slug}`);
|
||||||
}
|
}
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/contents`);
|
|
||||||
paths.push(`/${locale}/contents/${body.entry.slug}`);
|
|
||||||
if (body.entry.folder?.slug) {
|
|
||||||
paths.push(`/${locale}/contents/folder/${body.entry.folder.slug}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (body.entry.ranged_contents.length > 0) {
|
if (body.entry.ranged_contents.length > 0) {
|
||||||
body.entry.ranged_contents.forEach((ranged_content) => {
|
body.entry.ranged_contents.forEach((ranged_content) => {
|
||||||
const parentSlug = ranged_content.slug.slice(
|
const parentSlug = ranged_content.slug.slice(
|
||||||
|
@ -176,10 +166,6 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
|
||||||
);
|
);
|
||||||
paths.push(`/library/${parentSlug}`);
|
paths.push(`/library/${parentSlug}`);
|
||||||
paths.push(`/library/${parentSlug}/scans`);
|
paths.push(`/library/${parentSlug}/scans`);
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/library/${parentSlug}`);
|
|
||||||
paths.push(`/${locale}/library/${parentSlug}/scans`);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -188,9 +174,6 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
|
||||||
case "chronology-era":
|
case "chronology-era":
|
||||||
case "chronology-item": {
|
case "chronology-item": {
|
||||||
paths.push(`/wiki/chronology`);
|
paths.push(`/wiki/chronology`);
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/wiki/chronology`);
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,15 +185,6 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
|
||||||
if (body.entry.content) {
|
if (body.entry.content) {
|
||||||
paths.push(`/contents/${body.entry.content.slug}`);
|
paths.push(`/contents/${body.entry.content.slug}`);
|
||||||
}
|
}
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
if (body.entry.library_item) {
|
|
||||||
paths.push(`/${locale}/library/${body.entry.library_item.slug}`);
|
|
||||||
paths.push(`/${locale}/library/${body.entry.library_item.slug}/scans`);
|
|
||||||
}
|
|
||||||
if (body.entry.content) {
|
|
||||||
paths.push(`/${locale}/contents/${body.entry.content.slug}`);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,59 +200,31 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
|
||||||
paths.push(`/contents/folder/${subfolder.slug}`)
|
paths.push(`/contents/folder/${subfolder.slug}`)
|
||||||
);
|
);
|
||||||
body.entry.contents.forEach((content) => paths.push(`/contents/${content.slug}`));
|
body.entry.contents.forEach((content) => paths.push(`/contents/${content.slug}`));
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
if (body.entry.slug === "root") {
|
|
||||||
paths.push(`/${locale}/contents`);
|
|
||||||
}
|
|
||||||
paths.push(`/${locale}/contents/folder/${body.entry.slug}`);
|
|
||||||
if (body.entry.parent_folder) {
|
|
||||||
paths.push(`/${locale}/contents/folder/${body.entry.parent_folder.slug}`);
|
|
||||||
}
|
|
||||||
body.entry.subfolders.forEach((subfolder) =>
|
|
||||||
paths.push(`/${locale}/contents/folder/${subfolder.slug}`)
|
|
||||||
);
|
|
||||||
body.entry.contents.forEach((content) => paths.push(`/${locale}/contents/${content.slug}`));
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "wiki-page": {
|
case "wiki-page": {
|
||||||
paths.push(`/wiki`);
|
paths.push(`/wiki`);
|
||||||
paths.push(`/wiki/${body.entry.slug}`);
|
paths.push(`/wiki/${body.entry.slug}`);
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/wiki`);
|
|
||||||
paths.push(`/${locale}/wiki/${body.entry.slug}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "chronicle": {
|
case "chronicle": {
|
||||||
paths.push(`/chronicles`);
|
paths.push(`/chronicles`);
|
||||||
paths.push(`/chronicles/${body.entry.slug}`);
|
paths.push(`/chronicles/${body.entry.slug}`);
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/chronicles`);
|
|
||||||
paths.push(`/${locale}/chronicles/${body.entry.slug}`);
|
|
||||||
});
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "chronicles-chapter": {
|
case "chronicles-chapter": {
|
||||||
paths.push(`/chronicles`);
|
paths.push(`/chronicles`);
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/chronicles`);
|
|
||||||
});
|
|
||||||
body.entry.chronicles.forEach((chronicle) => {
|
body.entry.chronicles.forEach((chronicle) => {
|
||||||
paths.push(`/chronicles/${chronicle.slug}`);
|
paths.push(`/chronicles/${chronicle.slug}`);
|
||||||
i18n.locales.forEach((locale: string) => {
|
|
||||||
paths.push(`/${locale}/chronicles/${chronicle.slug}`);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case "custom": {
|
case "custom": {
|
||||||
paths.push(`${body.url}`);
|
paths.push(`${body.path}`);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -287,11 +233,14 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.table(paths);
|
const localizedPaths = cartesianProduct(i18n.locales, paths).map(
|
||||||
|
([locale, path]) => `/${locale}${path}`
|
||||||
|
);
|
||||||
|
console.table(localizedPaths);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Promise.all(
|
Promise.all(
|
||||||
paths.map(async (path) => {
|
localizedPaths.map(async (path) => {
|
||||||
await res.revalidate(path);
|
await res.revalidate(path);
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue