Dynamic Sitemaps with Contentful and Next.js a.k.a 'look ma no webhooks'

Mike Rispoli on July 09, 2019

Creating a sitemap.xml file was something that always nagged at me when working with headless content management systems. "What do you mean Content... [Read Full]
markdown guide

Hi!Great article, well done. Just a quick note, I think you're missing a piece as posts.items is flagged as not defined.

Maybe you want to add something like:

    const posts = await client.getEntries({
        content_type: 'post',
        limit: 1000,
        include: 1,

Hi Asher,

So for this section of code the posts.items will come back as undefined if you don't have a content_type of posts inside your Contentful account. You would probably want to wrap that in an if statement in production in case your content_type returns no items. But you will likely have multiple types you want to add here for example posts, pages, categories, etc. Anything that represents a page that you want to expose to google.


Great post Mike! Curious how you feel about the circular dependency issues in xmlbuilder? I need to implement a similar solution in a mono repo using rollup and running into circular issues.


Hi Aimee,

So I haven't run into circular issues with xmlbuilder with this implementation. However, I have run into this many times with Contentful. The most common foot gun when working in Contentful is having a Content type that also contains a reference to itself. This is typically something that happens if you want to say have a blog post with a list of recommended blog posts that also contain their own blog post recommendations. What I think you have to do for the site map is set the include level to one so it doesn't fetch deeply nested references. In general I try to avoid doing that type of referencing with Contentful now and rather use an additional query for a use case like recommended articles and recommended products etc.

I hope that helps a bit but I know circular references can be very frustrating.


Thanks for the reply Mike!

I found that the xmlbuilder library has some circular dependency issues, but they have been fixed in xmlbuilder2! We've handled our Contentful related issues with some safe stringifiying, and definitely learned that includes query param lesson the hard way :)


code of conduct - report abuse