DEV Community

Cover image for Leveraging the ecosystem

Posted on

Leveraging the ecosystem

Whenever I run into a new problem when working on a Gatsby project, my first instinct is always to check and see if a solution has been created by the community. A quick search reveals gatsby-plugin-sitemap, an officially-maintained plugin that solves this exact problem! 🎉

Let's install it, either using yarn or npm:

yarn add gatsby-plugin-sitemap
Enter fullscreen mode Exit fullscreen mode

Next, we can add it to our gatsby-config.js:

module.exports = {
  siteMetadata: {
    // ✂️
  plugins: [
    // ✂️
Enter fullscreen mode Exit fullscreen mode

Whenever we build our site, this plugin will generate a sitemap.xml file, alongside all the other files that Gatsby builds.

Critically, this plugin only runs when building for production. This means that you won't be able to test it when running in development mode. Let's build, and spin up a static server with serve:

yarn build && serve public
Enter fullscreen mode Exit fullscreen mode

serve is an NPM package that will serve the files on your local filesystem. If you've never used it before, you'll first need to install it with yarn add -g serve.
We pass public as an argument, since Gatsby builds into the /public directory; that's where all our static files will be served from.

You should now be able to open localhost:5000/sitemap.xml, and see a beautiful ugly XML document.

Discussion (0)