Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revalidate on demand #14235

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/**/*"]
59 changes: 59 additions & 0 deletions src/pages/api/revalidate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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 (err) {
console.error(`Error revalidating ${localePath}`, err)
throw new Error(`Error revalidating ${localePath}`)
}
})
)
}

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
Loading