andi hakim
andi hakim

Reputation: 35

sitemap for gatsby-starter-blog

currently i'm have issue with generating sitemap.xml for gatsby-starter-blog. http://localhost:8000/sitemap.xml still not found.

what i'm already doing :

siteMetadata: 
{
    siteUrl: 'http://www.localhost:8000',
  },
plugins: [ 
  {
    resolve: 'gatsby-plugin-sitemap',          
  },
]
const sm = require(`sitemap`)


//const pages = edge.node.frontmatter.path
function pagesToSitemap(pages) {
  const urls = pages.map((p) => {
    if (p.path !== undefined) {
      return {
        url: p.path,
        changefreq: 'daily',
        priority: 0.7
      }
    }
  })
  // remove undefined (template pages)
  return urls.filter(u => u !== undefined)
}

function generateSiteMap(pages) {
  const sitemap = sm.createSitemap({
    hostname: 'http://localhost:8000',
    cacheTime: '60000',
    urls: pagesToSitemap(pages),
  })  
  fs.writeFileSync(
    `${__dirname}/public/sitemap.xml`,
    sitemap.toString()
  )
}

exports.onPostBuild = ({pages, callback}) => {  
  generateSiteMap(pages)
  callback()
}

Upvotes: 3

Views: 794

Answers (1)

Kyle Mathews
Kyle Mathews

Reputation: 3278

onPostBuild is only called during the build process, not develop, which it looks like you haven't run yet. Try running gatsby build and your onPostBuild implementation will then be run.

Upvotes: 2

Related Questions