sitemap for gatsby-starter-blog

793 views Asked by At

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 :

  • npm install gatsby-plugin-sitemap
  • adding site meta
siteMetadata: 
{
    siteUrl: 'http://www.localhost:8000',
  },
plugins: [ 
  {
    resolve: 'gatsby-plugin-sitemap',          
  },
]
  • here my code for gatsby-node.js
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()
}
1

There are 1 answers

0
Kyle Mathews On BEST ANSWER

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.