#I want to let my SSG routes rebuild
1 messages · Page 1 of 1 (latest)
Hi @brisk nymph. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Yes, you can make a Static Site Generation (SSG) route regenerate from within the application using a feature called Incremental Static Regeneration (ISR). This feature allows you to create or update static pages at runtime.
Here's an example of how you can use ISR in a dynamic route:
// This function gets called at build time
export async function getStaticPaths() {
// Call an external API endpoint to get posts
const res = await fetch('<https://.../posts'>)
const posts = await res.json()
// Get the paths we want to pre-render based on posts
const paths = posts.map((post) => ({
params: { id: post.id },
}))
// We'll pre-render only these paths at build time.
// { fallback: false } means other routes should 404.
return { paths, fallback: false }
}
export default function Post({ post }) {
// Render post...
}
export async function getStaticPaths() {
// ...
}
// This also gets called at build time
export async function getStaticProps({ params }) {
// params contains the post `id`.
}
In the getStaticPaths function, you specify which paths you want to pre-render at build time. The fallback: false option means that other routes should return a 404 error.
In the getStaticProps function, you fetch the data for the post with the given id and use it to pre-render the page.
You can learn more about ISR in the Next.js documentation (https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration).
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@brisk nymph kapa.ai is still learning and improving, please let us know how it did by reacting below
is it possible to revalidatePath on pages router?
hello?
@low chasm is it possible to revalidatePath on pages router?
Let me check the knowledge base for you. One moment! :books:
Yes, it is possible to use revalidatePath with the pages router in Next.js. The revalidatePath function allows you to manually revalidate data and re-render the route segments below a specific path in a single operation.
Here's an example of how you can use revalidatePath:
import { revalidatePath } from 'next/cache'
revalidatePath('/')
You can use revalidatePath in two places depending on what you're trying to achieve:
- Route Handlers - to revalidate data in response to a third party event (e.g. webhook).
- Server Actions - to revalidate data after a user interaction (e.g. form submission, clicking a button).
However, it's important to note that revalidatePath only invalidates the cache when the included path is next visited. This means calling revalidatePath with a dynamic route segment will not immediately trigger many revalidations at once. The invalidation only happens when the path is next visited.