DEV Community

Cover image for Next.js on-demand revalidation is here! ๐Ÿš€

Next.js on-demand revalidation is here! ๐Ÿš€

pul on February 22, 2022

Next.js on-demand static regeneration is finally here! ๐Ÿ˜ This is a super cool feature that makes possible to refresh statically generate pages. T...
Collapse
 
danihengeveld profile image
Dani Hengeveld

Is there a way to revalidate multiple pages? Or all pages? Or would you just have to call .unstable_revalidate() multiple times with different paths?

Collapse
 
letanthanhtai profile image
Le Tan Thanh Tai

You can code api handler, let say pages/system/revalidate which can receive parameter like pageUrl. Then in the next.config.js, you can have a rewrite rule like this

{
          source: '/:refreshPage(.+?)/revalidate/',
          destination: '/api/system/revalidate?pageUrl=:refreshPage',
        }
Enter fullscreen mode Exit fullscreen mode

So now, on any page, if you call /hello/revalidate, it will revalidate your hello page :). If you want to revalidate multiple pages, you can have another page which do a batch revalidate or just a bunch of curl scripts to do it.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
shane_lan_4b4e784af60b780 profile image
shane lan

Great explanation of Next JS revalidation! We implemented this feature on video hug ai (our AI video SaaS) and it's been a game-changer for updating content dynamically. The on-demand revalidation saves us from unnecessary rebuilds while keeping content fresh.

Collapse
 
behzadha profile image
Behzad Haji Ahmadi

for me, on Vercel, the revalidate works on all route expect the root '/'. It raises error 500. what should I do?

Collapse
 
ositoozy profile image
Oscar Lodriguez

For me as well if I call it from the server side. But use it in a useEffect or client side context it won't fail.

Collapse
 
devhwann profile image
Jihwan Heo • Edited

Thanks for the detailed explanation of Next.jsrevalidation! We've implemented this feature in our project, and it's been incredibly useful for keeping our content up-to-date without unnecessary rebuilds. Appreciate you sharing this!

Collapse
 
swarnim_badholiya_071de73 profile image
Swarnim Badholiya

GREAT READ, FOR INTEGRATION VISIT : ajackus.com/hire-next-js-developer