From b344dd8f7dd694191a204cf244355ca1eb8222ce Mon Sep 17 00:00:00 2001 From: Robson Oliveira Date: Mon, 8 Jan 2024 23:40:26 -0300 Subject: [PATCH] feat: add Incremental Static Regeneration --- src/pages/api/revalidate.ts | 15 ++++++++++ src/pages/docs/tracks/[slug].tsx | 35 +++++++++++----------- src/pages/docs/tutorial/[slug].tsx | 33 ++++++++++---------- src/pages/updates/announcements/[slug].tsx | 34 ++++++++++----------- 4 files changed, 67 insertions(+), 50 deletions(-) create mode 100644 src/pages/api/revalidate.ts diff --git a/src/pages/api/revalidate.ts b/src/pages/api/revalidate.ts new file mode 100644 index 00000000..be6d9481 --- /dev/null +++ b/src/pages/api/revalidate.ts @@ -0,0 +1,15 @@ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export default async function handler(req: any, res: any) { + if (req.query.secret !== process.env.REVALIDATE_SECRET) { + return res.status(401).json({ message: 'Invalid token' }) + } + + try { + if (req.query.url) { + await res.revalidate(req.query.url) + return res.json({ revalidated: true }) + } + } catch (err) { + return res.status(500).send('Error revalidating') + } +} diff --git a/src/pages/docs/tracks/[slug].tsx b/src/pages/docs/tracks/[slug].tsx index 3f414443..89b3354d 100644 --- a/src/pages/docs/tracks/[slug].tsx +++ b/src/pages/docs/tracks/[slug].tsx @@ -44,7 +44,7 @@ import { localeType, } from 'utils/navigation-utils' import { MarkdownRenderer } from '@vtexdocs/components' -import { ParsedUrlQuery } from 'querystring' +// import { ParsedUrlQuery } from 'querystring' const docsPathsGLOBAL = await getTracksPaths('tracks') @@ -166,23 +166,23 @@ const TrackPage: NextPage = ({ } export const getStaticPaths: GetStaticPaths = async () => { - const slugs: { [slug: string]: { locale: string; path: string }[] } = - await getTracksPaths('tracks') - - const paths: ( - | string - | { - params: ParsedUrlQuery - locale?: string | undefined - } - )[] = [] - Object.entries(slugs).forEach(([slug, locales]) => { - locales.forEach(({ locale }) => { - paths.push({ params: { slug }, locale }) - }) - }) + // const slugs: { [slug: string]: { locale: string; path: string }[] } = + // await getTracksPaths('tracks') + + // const paths: ( + // | string + // | { + // params: ParsedUrlQuery + // locale?: string | undefined + // } + // )[] = [] + // Object.entries(slugs).forEach(([slug, locales]) => { + // locales.forEach(({ locale }) => { + // paths.push({ params: { slug }, locale }) + // }) + // }) return { - paths, + paths: [], fallback: 'blocking', } } @@ -439,6 +439,7 @@ export const getStaticProps: GetStaticProps = async ({ breadcrumbList, branch, }, + revalidate: 600, } } catch (error) { logger.error(`Error while processing ${path}\n${error}`) diff --git a/src/pages/docs/tutorial/[slug].tsx b/src/pages/docs/tutorial/[slug].tsx index 2ed45876..7a9214a7 100644 --- a/src/pages/docs/tutorial/[slug].tsx +++ b/src/pages/docs/tutorial/[slug].tsx @@ -44,7 +44,7 @@ import { getParents, localeType, } from 'utils/navigation-utils' -import { ParsedUrlQuery } from 'querystring' +// import { ParsedUrlQuery } from 'querystring' const docsPathsGLOBAL = await getTutorialsPaths('tutorials') @@ -166,23 +166,23 @@ const TutorialPage: NextPage = ({ } export const getStaticPaths: GetStaticPaths = async () => { - const slugs: { [slug: string]: { locale: string; path: string }[] } = - await getTutorialsPaths('tutorials') + // const slugs: { [slug: string]: { locale: string; path: string }[] } = + // await getTutorialsPaths('tutorials') - const paths: ( - | string - | { - params: ParsedUrlQuery - locale?: string | undefined - } - )[] = [] - Object.entries(slugs).forEach(([slug, locales]) => { - locales.forEach(({ locale }) => { - paths.push({ params: { slug }, locale }) - }) - }) + // const paths: ( + // | string + // | { + // params: ParsedUrlQuery + // locale?: string | undefined + // } + // )[] = [] + // Object.entries(slugs).forEach(([slug, locales]) => { + // locales.forEach(({ locale }) => { + // paths.push({ params: { slug }, locale }) + // }) + // }) return { - paths, + paths: [], fallback: 'blocking', } } @@ -436,6 +436,7 @@ export const getStaticProps: GetStaticProps = async ({ breadcrumbList, branch, }, + revalidate: 600, } } catch (error) { logger.error(`Error while processing ${path}\n${error}`) diff --git a/src/pages/updates/announcements/[slug].tsx b/src/pages/updates/announcements/[slug].tsx index 0d9b4a27..70f793e3 100644 --- a/src/pages/updates/announcements/[slug].tsx +++ b/src/pages/updates/announcements/[slug].tsx @@ -40,7 +40,7 @@ import { ActionType, getAction } from 'components/announcement-card/functions' import styles from 'styles/documentation-page' import { PreviewContext } from 'utils/contexts/preview' -import { ParsedUrlQuery } from 'querystring' +// import { ParsedUrlQuery } from 'querystring' const docsPathsGLOBAL = await getNewsPaths('announcements') @@ -126,24 +126,24 @@ const NewsPage: NextPage = ({ serialized, branch }) => { } export const getStaticPaths: GetStaticPaths = async () => { - const slugs: { [slug: string]: { locale: string; path: string }[] } = - await getNewsPaths('announcements') - - const paths: ( - | string - | { - params: ParsedUrlQuery - locale?: string | undefined - } - )[] = [] - Object.entries(slugs).forEach(([slug, locales]) => { - locales.forEach(({ locale }) => { - paths.push({ params: { slug }, locale }) - }) - }) + // const slugs: { [slug: string]: { locale: string; path: string }[] } = + // await getNewsPaths('announcements') + + // const paths: ( + // | string + // | { + // params: ParsedUrlQuery + // locale?: string | undefined + // } + // )[] = [] + // Object.entries(slugs).forEach(([slug, locales]) => { + // locales.forEach(({ locale }) => { + // paths.push({ params: { slug }, locale }) + // }) + // }) return { - paths, + paths: [], fallback: 'blocking', } }