@astrojs/sitemap: What It Is and How to Use It
@astrojs/sitemap is an official Astro integration that automatically generates a sitemap.xml file for your website. It helps search engines find and index your pages by listing all your site URLs in a structured format.How It Works
@astrojs/sitemap works by scanning your Astro project’s pages and collecting their URLs. It then creates a sitemap.xml file that lists these URLs in a way search engines understand. Think of it like a map for a treasure hunt, where the treasure is your website’s pages and the map helps search engines find them easily.
When you build your Astro site, this integration runs automatically and updates the sitemap with all your current pages. This means you don’t have to manually create or update the sitemap every time you add or remove pages. It keeps your site friendly for search engines without extra work.
Example
This example shows how to add @astrojs/sitemap to your Astro project and configure it to generate a sitemap.
import { defineConfig } from 'astro/config'; import sitemap from '@astrojs/sitemap'; export default defineConfig({ integrations: [ sitemap({ filter: (page) => !page.includes('/drafts/'), hostname: 'https://example.com' }) ] });
When to Use
Use @astrojs/sitemap whenever you want to improve your website’s SEO by helping search engines discover your pages easily. It is especially useful for sites with many pages or frequently updated content.
For example, blogs, portfolios, or e-commerce sites benefit from automatic sitemap generation because it keeps search engines updated without manual effort. It also helps new pages get indexed faster, improving your site’s visibility in search results.
Key Points
@astrojs/sitemapautomatically creates a sitemap.xml for your Astro site.- It updates the sitemap on every build, reflecting your current pages.
- You can filter pages to exclude from the sitemap.
- It requires setting your site’s hostname for correct URLs.
- Improves SEO by making your site easier to crawl.
Key Takeaways
@astrojs/sitemap generates a sitemap.xml automatically for Astro projects.