Setting up a sitemap is essential for optimizing a website built with Astro for search engines. By properly configuring a sitemap, you make it easier for search engines to understand your site's structure, supporting your SEO efforts.
In this article, we explain how to add a sitemap in Astro and discuss key considerations during setup. Master the basics of sitemap configuration and build a website that stands out to search engines!
What is an Astro sitemap?
A sitemap is a file that hierarchically displays the page structure of a website. By reading the sitemap, search engine crawlers can understand the overall layout of the site and index pages efficiently.
Astro is a framework that enables building high-speed websites as a static site generator. Even for sites built with Astro, configuring a sitemap remains a crucial element. Proper sitemap setup facilitates indexing by search engines, leading to improved SEO.
How to add a sitemap in Astro
To add a sitemap in Astro, follow these steps.
1. Installing the sitemap generation plugin
First, install Astro's sitemap generation plugin, "astro-sitemap." Run the following command in your project's root directory.
npm install astro-sitemap
2. Creating the sitemap configuration file
Next, create a sitemap.config.js file in the root directory of your project and write the sitemap settings.
// sitemap.config.js
export default {
siteUrl: 'https://gleamhub.net/media',
generateRobotsTxt: true,
exclude: ['/404'],
}
siteUrl: Site URLgenerateRobotsTxt: Whether to automatically generaterobots.txtexclude: An array specifying pages to exclude from the sitemap
3. Generating and verifying the sitemap
Once the configuration file is created, run Astro's build command to generate the sitemap.
npm run build
When the build finishes, the sitemap-index.xml file will be generated in your project's dist directory. Uploading this file to your server enables search engines to discover your sitemap.
Key considerations when adding a sitemap
1. Configuring pages to include and exclude from the sitemap
There are pages you should include in your sitemap and pages you should omit.
- Pages to include: Pages you want indexed
- Pages to exclude: Unpublished pages, duplicate content, etc.
Leverage the exclude option in sitemap.config.js to carefully choose the appropriate pages.
2. Setting sitemap update frequency
It is important to adjust the update frequency of your sitemap to align with how often your site is updated.
While sitemap.config.js does not provide an option to configure update frequencies, make sure to run a build and regenerate it whenever the site updates.
Conclusion
We have explored how to add a sitemap in Astro. Sitemaps play an essential role in helping search engine crawlers understand your site structure.
- Install the
astro-sitemapplugin - Configure it properly in
sitemap.config.js - Automatically generate
sitemap-index.xmlduring build
Pay attention to page selection and update frequency to build an SEO-effective sitemap.
Setting up a sitemap is the first step in search engine optimization. Take advantage of Astro's capabilities to build a website that serves both users and search engines.
Related articles
- [Astro] How to set up GA tracking tags only in production — Next after sitemaps is web analytics. GA4 setup explained
- [Astro] How to set up GA tracking tags only in production — Setup guide to prevent sending unnecessary data from development environments








