Next.js Discord

Discord Forum

Revalidate path for multiple in SSG app router

Unanswered
Ojos Azules posted this in #help-forum
Open in Discord
Ojos AzulesOP
I’m working on an SSG app router and need to handle content updates from the backend by hitting the revalidate API (/api/revalidate/route) with a path array. However, I’ve encountered an issue where I can’t revalidate multiple paths in sequence. For example:
revalidatePath("/all-courses")
revalidatePath("/course/[slug]")

It only processes the first path.

I read the Next.js documentation and tried using revalidatePath with specific route layouts, like:

import { revalidatePath } from 'next/cache'
revalidatePath('/blog/[slug]', 'layout')
// or with route groups
revalidatePath('/(main)/post/[slug]', 'layout')


However, my scenario requires revalidating two independent routes:

 
    all-blogs/page 
    blog/[id]/page


The current approach isn’t working for these independent routes. Here’s the API code I’m using:

const revalidatePromises = (paths as string[]).map(async (path) => {
  console.log('Revalidating path:', path);

  // Revalidate the specific path
  const revalidateCurrent = path.startsWith('/course/')
    ? revalidatePath('/[[slug]]', 'page')
    : revalidatePath(path);

  // Await both revalidation promises
  await Promise.all([revalidateCurrent]);
});

await Promise.all(revalidatePromises);

return NextResponse.json({
  revalidated: true,
  message: 'Paths revalidated',
});


Is there a way to revalidate multiple independent routes effectively? If not, would it be better to revalidate all pages as a fallback? I’m open to any suggestions for improving this.

1 Reply

Ojos AzulesOP
thanks for the response, I've updated the question with formatting,
sandbox: https://codesandbox.io/p/devbox/dhgkkg , go to api/revalidate/route.ts