Skip to content

Commit

Permalink
add revalidate endpoint for on demand revalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
pettinarip committed Oct 25, 2024
1 parent 10ed40f commit b90ffd4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 15 deletions.
2 changes: 1 addition & 1 deletion netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@
path = "en/developers/tutorials/creating-a-wagmi-ui-for-your-contract/"

[functions]
included_files = ["src/data/mocks/**/*"]
included_files = ["i18n.config.json","src/data/mocks/**/*"]
58 changes: 58 additions & 0 deletions src/pages/api/revalidate.ts
Original file line number Diff line number Diff line change
@@ -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")
}
}
4 changes: 1 addition & 3 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down Expand Up @@ -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<Props>

Expand Down
5 changes: 1 addition & 4 deletions src/pages/stablecoins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]>(
[
Expand Down Expand Up @@ -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<Props>

Expand Down
5 changes: 1 addition & 4 deletions src/pages/staking/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand All @@ -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<Props>

Expand Down
3 changes: 0 additions & 3 deletions src/pages/wallets/find-wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<Props>

Expand Down

0 comments on commit b90ffd4

Please sign in to comment.