From b90ffd44d8343e2f37acf295941ff3bef381637e Mon Sep 17 00:00:00 2001 From: Pablo Pettinari Date: Thu, 24 Oct 2024 12:29:51 +0200 Subject: [PATCH 1/2] add revalidate endpoint for on demand revalidation --- netlify.toml | 2 +- src/pages/api/revalidate.ts | 58 +++++++++++++++++++++++++++++++ src/pages/index.tsx | 4 +-- src/pages/stablecoins.tsx | 5 +-- src/pages/staking/index.tsx | 5 +-- src/pages/wallets/find-wallet.tsx | 3 -- 6 files changed, 62 insertions(+), 15 deletions(-) create mode 100644 src/pages/api/revalidate.ts diff --git a/netlify.toml b/netlify.toml index 4bb51b46e37..83bdfa26db2 100644 --- a/netlify.toml +++ b/netlify.toml @@ -39,4 +39,4 @@ path = "en/developers/tutorials/creating-a-wagmi-ui-for-your-contract/" [functions] - included_files = ["src/data/mocks/**/*"] \ No newline at end of file + included_files = ["i18n.config.json","src/data/mocks/**/*"] \ No newline at end of file diff --git a/src/pages/api/revalidate.ts b/src/pages/api/revalidate.ts new file mode 100644 index 00000000000..4fda1c6c8ac --- /dev/null +++ b/src/pages/api/revalidate.ts @@ -0,0 +1,58 @@ +import type { NextApiRequest, NextApiResponse } from "next" + +import i18nConfig from "../../../i18n.config.json" + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + if (req.query.secret !== process.env.REVALIDATE_SECRET) { + return res.status(401).json({ message: "Invalid secret" }) + } + + const BUILD_LOCALES = process.env.BUILD_LOCALES + // Supported locales defined in `i18n.config.json` + const locales = BUILD_LOCALES + ? BUILD_LOCALES.split(",") + : i18nConfig.map(({ code }) => code) + + const path = req.query.path as string + console.log("Revalidating", path) + + try { + if (!path) { + return res.status(400).json({ message: "No path provided" }) + } + + const hasLocaleInPath = locales.some((locale) => + path.startsWith(`/${locale}/`) + ) + + if (hasLocaleInPath) { + await res.revalidate(path) + } else { + // First revalidate the default locale to cache the results + await res.revalidate(`/en${path}`) + + // Then revalidate all other locales + await Promise.all( + locales.map(async (locale) => { + const localePath = `/${locale}${path}` + console.log(`Revalidating ${localePath}`) + try { + await res.revalidate(localePath) + } catch (error) { + console.error(`Error revalidating ${localePath}:`, error) + } + }) + ) + } + + return res.json({ revalidated: true }) + } catch (err) { + console.error(err) + // If there was an error, Next.js will continue + // to show the last successfully generated page + return res.status(500).send("Error revalidating") + } +} diff --git a/src/pages/index.tsx b/src/pages/index.tsx index ef5bc668448..54913b5c14d 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -119,7 +119,7 @@ type Props = BasePageProps & { } // In seconds -const REVALIDATE_TIME = BASE_TIME_UNIT * 24 +const REVALIDATE_TIME = BASE_TIME_UNIT * 1 const loadData = dataLoader( [ @@ -193,8 +193,6 @@ export const getStaticProps = (async ({ locale }) => { metricResults, rssData: { rssItems, blogLinks }, }, - // TODO: re-enable revalidation once we have a workaround for failing builds - // revalidate: BASE_TIME_UNIT * 24, } }) satisfies GetStaticProps diff --git a/src/pages/stablecoins.tsx b/src/pages/stablecoins.tsx index 98d1be9e09c..c8b5df2c569 100644 --- a/src/pages/stablecoins.tsx +++ b/src/pages/stablecoins.tsx @@ -92,7 +92,7 @@ type Props = BasePageProps & { } // In seconds -const REVALIDATE_TIME = BASE_TIME_UNIT * 24 * 7 +const REVALIDATE_TIME = BASE_TIME_UNIT * 1 const loadData = dataLoader<[EthereumDataResponse, StablecoinDataResponse]>( [ @@ -191,9 +191,6 @@ export const getStaticProps = (async ({ locale }) => { markets, marketsHasError, }, - // Updated once a week - // TODO: re-enable revalidation once we have a workaround for failing builds - // revalidate: BASE_TIME_UNIT * 24 * 7, } }) satisfies GetStaticProps diff --git a/src/pages/staking/index.tsx b/src/pages/staking/index.tsx index de8665d47e4..93b5a7b8b4b 100644 --- a/src/pages/staking/index.tsx +++ b/src/pages/staking/index.tsx @@ -154,7 +154,7 @@ type Props = BasePageProps & { } // In seconds -const REVALIDATE_TIME = BASE_TIME_UNIT * 24 +const REVALIDATE_TIME = BASE_TIME_UNIT * 1 const loadData = dataLoader( [["stakingStatsData", fetchBeaconchainData]], @@ -181,9 +181,6 @@ export const getStaticProps = (async ({ locale }) => { data, lastDeployLocaleTimestamp, }, - // Updated once a day - // TODO: re-enable revalidation once we have a workaround for failing builds - // revalidate: BASE_TIME_UNIT * 24, } }) satisfies GetStaticProps diff --git a/src/pages/wallets/find-wallet.tsx b/src/pages/wallets/find-wallet.tsx index e3faee8a612..503b7769b80 100644 --- a/src/pages/wallets/find-wallet.tsx +++ b/src/pages/wallets/find-wallet.tsx @@ -77,9 +77,6 @@ export const getStaticProps = (async ({ locale }) => { lastDeployLocaleTimestamp, wallets, }, - // Updated once a day - // TODO: re-enable revalidation once we have a workaround for failing builds - // revalidate: BASE_TIME_UNIT * 24, } }) satisfies GetStaticProps From 48226cc9d4268661eeb40802a64e695988a6579e Mon Sep 17 00:00:00 2001 From: Pablo Pettinari Date: Fri, 25 Oct 2024 17:54:31 +0200 Subject: [PATCH 2/2] throw error when locale revalidation fails --- src/pages/api/revalidate.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/api/revalidate.ts b/src/pages/api/revalidate.ts index 4fda1c6c8ac..1d1451dfd6a 100644 --- a/src/pages/api/revalidate.ts +++ b/src/pages/api/revalidate.ts @@ -41,8 +41,9 @@ export default async function handler( console.log(`Revalidating ${localePath}`) try { await res.revalidate(localePath) - } catch (error) { - console.error(`Error revalidating ${localePath}:`, error) + } catch (err) { + console.error(`Error revalidating ${localePath}`, err) + throw new Error(`Error revalidating ${localePath}`) } }) )