Generate sitemap dynamically or on the fly

Hello,

I would like to generate a sitemap on the fly, without having to rebuild the entire application.

Say I add a product to my store, that product information is pulled into the sitemap on build by querying the product database.

Is there a way I can call a regenerateSitemap method after successful product creation?

Hi @lofiguy, welcome to the Vercel Community!

I see. I think you can create a Route Handler for the sitemap route and return an XML response by pulling the latest information from your database.

Have you already tried this?

I have not. The implementation isn’t as much as my concern as the logic, however. I can make the call in a router handler, server action, etc. My issue lies mostly within whether we have access to the functionality used to generate the sitemap that NextJS uses internally.

I wanted to avoid reinventing the wheel so to speak.

If not, then I will just make a service to override the sitemap generated by NextJS and call that via cron & after certain events are fired that would call for it to be refreshed.