Next13 + updated deps + fixed revalidation

This commit is contained in:
DrMint 2022-11-13 00:35:13 +01:00
parent 35b58982d0
commit c356679813
7 changed files with 695 additions and 550 deletions

1000
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -5,7 +5,7 @@
"dev": "next dev -p 12499", "dev": "next dev -p 12499",
"precommit": "npm run fetch-local-data && npm run prettier && npm run unused-exports && npm run eslint && npm run tsc && echo ALL PRECOMMIT CHECKS PASSED SUCCESSFULLY, LET\\'S FUCKING GO!", "precommit": "npm run fetch-local-data && npm run prettier && npm run unused-exports && npm run eslint && npm run tsc && echo ALL PRECOMMIT CHECKS PASSED SUCCESSFULLY, LET\\'S FUCKING GO!",
"unused-exports": "ts-unused-exports ./tsconfig.json --excludePathsFromReport=src/pages --ignoreFiles=generated", "unused-exports": "ts-unused-exports ./tsconfig.json --excludePathsFromReport=src/pages --ignoreFiles=generated",
"fetch-local-data": "npm run generate && esrun src/graphql/fetchLocalData.ts", "fetch-local-data": "npm run generate && esrun src/graphql/fetchLocalData.ts --esrun",
"prebuild": "npm run fetch-local-data", "prebuild": "npm run fetch-local-data",
"build": "next build", "build": "next build",
"postbuild": "next-sitemap --config next-sitemap.config.js", "postbuild": "next-sitemap --config next-sitemap.config.js",
@ -27,11 +27,11 @@
"autoprefixer": "^10.4.13", "autoprefixer": "^10.4.13",
"cuid": "^2.1.8", "cuid": "^2.1.8",
"graphql-request": "^5.0.0", "graphql-request": "^5.0.0",
"jotai": "^1.9.0", "jotai": "^1.9.2",
"markdown-to-jsx": "^7.1.7", "markdown-to-jsx": "^7.1.7",
"next": "^12.3.1", "next": "^13.0.3",
"nodemailer": "^6.8.0", "nodemailer": "^6.8.0",
"rc-slider": "^10.0.1", "rc-slider": "^10.1.0",
"react": "18.2.0", "react": "18.2.0",
"react-dom": "18.2.0", "react-dom": "18.2.0",
"react-hotkeys-hook": "^3.4.7", "react-hotkeys-hook": "^3.4.7",
@ -46,29 +46,29 @@
}, },
"devDependencies": { "devDependencies": {
"@digitak/esrun": "^3.2.14", "@digitak/esrun": "^3.2.14",
"@graphql-codegen/cli": "^2.13.8", "@graphql-codegen/cli": "^2.13.11",
"@graphql-codegen/typescript": "2.8.0", "@graphql-codegen/typescript": "2.8.1",
"@graphql-codegen/typescript-graphql-request": "^4.5.7", "@graphql-codegen/typescript-graphql-request": "^4.5.8",
"@graphql-codegen/typescript-operations": "^2.5.5", "@graphql-codegen/typescript-operations": "^2.5.6",
"@types/node": "18.11.7", "@types/node": "18.11.9",
"@types/nodemailer": "^6.4.6", "@types/nodemailer": "^6.4.6",
"@types/react": "18.0.22", "@types/react": "^18.0.22",
"@types/react-dom": "^18.0.8", "@types/react-dom": "^18.0.8",
"@types/string-natural-compare": "^3.0.2", "@types/string-natural-compare": "^3.0.2",
"@types/throttle-debounce": "^5.0.0", "@types/throttle-debounce": "^5.0.0",
"@types/turndown": "^5.0.1", "@types/turndown": "^5.0.1",
"@types/ua-parser-js": "^0.7.36", "@types/ua-parser-js": "^0.7.36",
"@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/eslint-plugin": "^5.42.1",
"@typescript-eslint/parser": "^5.41.0", "@typescript-eslint/parser": "^5.42.1",
"dotenv": "^16.0.3", "dotenv": "^16.0.3",
"eslint": "^8.26.0", "eslint": "^8.27.0",
"eslint-config-next": "12.3.1", "eslint-config-next": "13.0.3",
"eslint-plugin-import": "^2.26.0", "eslint-plugin-import": "^2.26.0",
"graphql": "^16.6.0", "graphql": "^16.6.0",
"next-sitemap": "^3.1.29", "next-sitemap": "^3.1.30",
"prettier": "^2.7.1", "prettier": "^2.7.1",
"prettier-plugin-tailwindcss": "^0.1.13", "prettier-plugin-tailwindcss": "^0.1.13",
"tailwindcss": "^3.2.1", "tailwindcss": "^3.2.4",
"ts-unused-exports": "^8.0.0", "ts-unused-exports": "^8.0.0",
"typescript": "^4.8.4" "typescript": "^4.8.4"
}, },

View File

@ -1 +1 @@
{"currencies":{"data":[{"id":"1","attributes":{"code":"EUR","symbol":"€","rate_to_usd":1.1062771,"display_decimals":true}},{"id":"2","attributes":{"code":"CAD","symbol":"$","rate_to_usd":0.79319156,"display_decimals":true}},{"id":"3","attributes":{"code":"USD","symbol":"$","rate_to_usd":1,"display_decimals":true}},{"id":"4","attributes":{"code":"JPY","symbol":"¥","rate_to_usd":0.0083864261,"display_decimals":false}},{"id":"5","attributes":{"code":"BRL","symbol":"R$","rate_to_usd":0.19904328,"display_decimals":true}},{"id":"6","attributes":{"code":"GBP","symbol":"£","rate_to_usd":1.3181323,"display_decimals":true}},{"id":"7","attributes":{"code":"AUD","symbol":"$","rate_to_usd":0.7422,"display_decimals":true}},{"id":"8","attributes":{"code":"INR","symbol":"₹","rate_to_usd":0.013162881,"display_decimals":false}},{"id":"9","attributes":{"code":"NZD","symbol":"$","rate_to_usd":0.69089984,"display_decimals":true}},{"id":"10","attributes":{"code":"CHF","symbol":"CHF","rate_to_usd":1.0728706,"display_decimals":true}}]}} {"currencies":{"data":[{"id":"1","attributes":{"code":"EUR","symbol":"€","rate_to_usd":1.036166,"display_decimals":true}},{"id":"2","attributes":{"code":"CAD","symbol":"$","rate_to_usd":0.79319156,"display_decimals":true}},{"id":"3","attributes":{"code":"USD","symbol":"$","rate_to_usd":1,"display_decimals":true}},{"id":"4","attributes":{"code":"JPY","symbol":"¥","rate_to_usd":0.0083864261,"display_decimals":false}},{"id":"5","attributes":{"code":"BRL","symbol":"R$","rate_to_usd":0.19904328,"display_decimals":true}},{"id":"6","attributes":{"code":"GBP","symbol":"£","rate_to_usd":1.3181323,"display_decimals":true}},{"id":"7","attributes":{"code":"AUD","symbol":"$","rate_to_usd":0.7422,"display_decimals":true}},{"id":"8","attributes":{"code":"INR","symbol":"₹","rate_to_usd":0.013162881,"display_decimals":false}},{"id":"9","attributes":{"code":"NZD","symbol":"$","rate_to_usd":0.69089984,"display_decimals":true}},{"id":"10","attributes":{"code":"CHF","symbol":"CHF","rate_to_usd":1.0728706,"display_decimals":true}}]}}

File diff suppressed because one or more lines are too long

View File

@ -21,13 +21,16 @@ const readLocalData = <T>(name: LocalDataFile): T => {
return JSON.parse(readFileSync(path, { encoding: "utf8" })); return JSON.parse(readFileSync(path, { encoding: "utf8" }));
}; };
const sdk = getReadySdk(); export const fetchLocalData = async (): Promise<void> => {
const sdk = getReadySdk();
(async () => {
writeLocalData("websiteInterfaces", await sdk.localDataGetWebsiteInterfaces()); writeLocalData("websiteInterfaces", await sdk.localDataGetWebsiteInterfaces());
writeLocalData("currencies", await sdk.localDataGetCurrencies()); writeLocalData("currencies", await sdk.localDataGetCurrencies());
writeLocalData("languages", await sdk.localDataGetLanguages()); writeLocalData("languages", await sdk.localDataGetLanguages());
})(); };
if (process.argv[2] === "--esrun") {
fetchLocalData();
}
// ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ // ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─

View File

@ -0,0 +1,22 @@
query revalidationGetRangedContent($id: ID) {
rangedContent(id: $id) {
data {
attributes {
content {
data {
attributes {
slug
}
}
}
library_item {
data {
attributes {
slug
}
}
}
}
}
}
}

View File

@ -1,6 +1,8 @@
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"; import { cartesianProduct, filterHasAttributes } from "helpers/others";
import { fetchLocalData } from "graphql/fetchLocalData";
import { getReadySdk } from "graphql/sdk";
type CRUDEvents = "entry.create" | "entry.delete" | "entry.update"; type CRUDEvents = "entry.create" | "entry.delete" | "entry.update";
@ -10,6 +12,8 @@ type StrapiEvent = {
entry: Record<string, unknown>; entry: Record<string, unknown>;
}; };
type StrapiRelationalField = { count: number };
type RequestProps = type RequestProps =
| CustomRequest | CustomRequest
| StrapiChronicle | StrapiChronicle
@ -17,9 +21,12 @@ type RequestProps =
| StrapiChronology | StrapiChronology
| StrapiContent | StrapiContent
| StrapiContentFolder | StrapiContentFolder
| StrapiCurrency
| StrapiLanguage
| StrapiLibraryItem | StrapiLibraryItem
| StrapiPostContent | StrapiPostContent
| StrapiRangedContent | StrapiRangedContent
| StrapiWebsiteInterface
| StrapiWiki; | StrapiWiki;
interface CustomRequest { interface CustomRequest {
@ -31,12 +38,9 @@ interface StrapiRangedContent extends StrapiEvent {
event: CRUDEvents; event: CRUDEvents;
model: "ranged-content"; model: "ranged-content";
entry: { entry: {
library_item?: { id: string;
slug: string; library_item: StrapiRelationalField;
}; content: StrapiRelationalField;
content?: {
slug: string;
};
}; };
} }
@ -44,12 +48,8 @@ interface StrapiContent extends StrapiEvent {
model: "content"; model: "content";
entry: { entry: {
slug: string; slug: string;
folder?: { folder: StrapiRelationalField;
slug: string; ranged_contents: StrapiRelationalField;
};
ranged_contents: {
slug: string;
}[];
}; };
} }
@ -61,16 +61,36 @@ interface StrapiPostContent extends StrapiEvent {
}; };
} }
interface StrapiWebsiteInterface extends StrapiEvent {
event: CRUDEvents;
model: "website-interface";
entry: {
slug: string;
};
}
interface StrapiLanguage extends StrapiEvent {
event: CRUDEvents;
model: "language";
entry: {
slug: string;
};
}
interface StrapiCurrency extends StrapiEvent {
event: CRUDEvents;
model: "currency";
entry: {
slug: string;
};
}
interface StrapiLibraryItem extends StrapiEvent { interface StrapiLibraryItem extends StrapiEvent {
event: CRUDEvents; event: CRUDEvents;
model: "library-item"; model: "library-item";
entry: { entry: {
slug: string; slug: string;
subitem_of: [ subitem_of: StrapiRelationalField;
{
slug: string;
}
];
}; };
} }
@ -79,13 +99,9 @@ interface StrapiContentFolder extends StrapiEvent {
model: "contents-folder"; model: "contents-folder";
entry: { entry: {
slug: string; slug: string;
parent_folder?: { parent_folder: StrapiRelationalField;
slug: string; subfolders: StrapiRelationalField;
}; contents: StrapiRelationalField;
subfolders: { slug: string }[];
contents: {
slug: string;
}[];
}; };
} }
@ -118,13 +134,17 @@ interface StrapiChronicleChapter extends StrapiEvent {
}; };
} }
type ResponseMailProps = { type ResponseProps = {
message: string; message: string;
revalidated: boolean; revalidated: boolean;
}; };
const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>): void => { const Revalidate = async (
req: NextApiRequest,
res: NextApiResponse<ResponseProps>
): Promise<void> => {
const body = req.body as RequestProps; const body = req.body as RequestProps;
const sdk = getReadySdk();
// Check for secret to confirm this is a valid request // Check for secret to confirm this is a valid request
if (req.headers.authorization !== `Bearer ${process.env.REVALIDATION_TOKEN}`) { if (req.headers.authorization !== `Bearer ${process.env.REVALIDATION_TOKEN}`) {
@ -134,6 +154,8 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
const paths: string[] = []; const paths: string[] = [];
console.log(body);
switch (body.model) { switch (body.model) {
case "post": { case "post": {
paths.push(`/news`); paths.push(`/news`);
@ -145,9 +167,17 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
paths.push(`/library`); paths.push(`/library`);
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`);
body.entry.subitem_of.forEach((parentItem) => {
paths.push(`/library/${parentItem.slug}`); if (body.entry.subitem_of.count > 0) {
}); const libraryItem = await sdk.getLibraryItem({
language_code: "en",
slug: body.entry.slug,
});
filterHasAttributes(libraryItem.libraryItems?.data[0].attributes?.subitem_of?.data, [
"attributes.slug",
] as const).forEach((parentItem) => paths.push(`/library/${parentItem.attributes.slug}`));
}
break; break;
} }
@ -155,15 +185,22 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
paths.push(`/contents`); paths.push(`/contents`);
paths.push(`/contents/all`); paths.push(`/contents/all`);
paths.push(`/contents/${body.entry.slug}`); paths.push(`/contents/${body.entry.slug}`);
if (body.entry.folder?.slug) {
paths.push(`/contents/folder/${body.entry.folder.slug}`); if (body.entry.folder.count > 0 || body.entry.ranged_contents.count > 0) {
} const content = await sdk.getContentText({
if (body.entry.ranged_contents.length > 0) { language_code: "en",
body.entry.ranged_contents.forEach((ranged_content) => { slug: body.entry.slug,
const parentSlug = ranged_content.slug.slice( });
0,
ranged_content.slug.length - body.entry.slug.length - 1 const folderSlug = content.contents?.data[0].attributes?.folder?.data?.attributes?.slug;
); if (folderSlug) {
paths.push(`/contents/folder/${folderSlug}`);
}
filterHasAttributes(content.contents?.data[0].attributes?.ranged_contents?.data, [
"attributes.library_item.data.attributes.slug",
] as const).forEach((ranged_content) => {
const parentSlug = ranged_content.attributes.library_item.data.attributes.slug;
paths.push(`/library/${parentSlug}`); paths.push(`/library/${parentSlug}`);
paths.push(`/library/${parentSlug}/reader`); paths.push(`/library/${parentSlug}/reader`);
}); });
@ -178,12 +215,21 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
} }
case "ranged-content": { case "ranged-content": {
if (body.entry.library_item) { if (body.entry.content.count > 0 || body.entry.library_item.count > 0) {
paths.push(`/library/${body.entry.library_item.slug}`); const rangedContent = await sdk.revalidationGetRangedContent({
paths.push(`/library/${body.entry.library_item.slug}/reader`); id: body.entry.id,
} });
if (body.entry.content) { const libraryItemSlug =
paths.push(`/contents/${body.entry.content.slug}`); rangedContent.rangedContent?.data?.attributes?.content?.data?.attributes?.slug;
if (libraryItemSlug) {
paths.push(`/library/${libraryItemSlug}`);
paths.push(`/library/${libraryItemSlug}/reader`);
}
const contentSlug =
rangedContent.rangedContent?.data?.attributes?.content?.data?.attributes?.slug;
if (contentSlug) {
paths.push(`/contents/${contentSlug}`);
}
} }
break; break;
} }
@ -193,13 +239,30 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
paths.push(`/contents`); paths.push(`/contents`);
} }
paths.push(`/contents/folder/${body.entry.slug}`); paths.push(`/contents/folder/${body.entry.slug}`);
if (body.entry.parent_folder) {
paths.push(`/contents/folder/${body.entry.parent_folder.slug}`); if (
body.entry.contents.count > 0 ||
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 (parentSlug) {
paths.push(`/contents/folder/${parentSlug}`);
}
filterHasAttributes(folder.contentsFolders?.data[0].attributes?.subfolders?.data, [
"attributes.slug",
] as const).forEach((subfolder) =>
paths.push(`/contents/folder/${subfolder.attributes.slug}`)
);
filterHasAttributes(folder.contentsFolders?.data[0].attributes?.contents?.data, [
"attributes.slug",
] as const).forEach((content) => paths.push(`/contents/${content.attributes.slug}`));
} }
body.entry.subfolders.forEach((subfolder) =>
paths.push(`/contents/folder/${subfolder.slug}`)
);
body.entry.contents.forEach((content) => paths.push(`/contents/${content.slug}`));
break; break;
} }
@ -223,20 +286,27 @@ const Revalidate = (req: NextApiRequest, res: NextApiResponse<ResponseMailProps>
break; break;
} }
case "website-interface":
case "language":
case "currency": {
await fetchLocalData();
break;
}
case "custom": { case "custom": {
paths.push(`${body.path}`); paths.push(`${body.path}`);
break; break;
} }
default: default:
console.log(body); console.log("Unknown case");
break; break;
} }
console.table(paths);
const localizedPaths = cartesianProduct(i18n.locales, paths).map( const localizedPaths = cartesianProduct(i18n.locales, paths).map(
([locale, path]) => `/${locale}${path}` ([locale, path]) => `/${locale}${path}`
); );
console.table(localizedPaths);
try { try {
Promise.all( Promise.all(