🛠️ Sitemap (Official)

Add Astro’s official sitemap and customize URLs, changefreq, priority, and lastmod.

🗺️ Sitemap (Official)

THIS IS ALREADY INCLUDED WITH THE THEME, THESE ARE JUST INSTRUCTIONS ON HOW TO INSTALL / USE

MAKE SURE YOUR CORRECT WEBSITE URL IS INCLUDED IN THE astro.config.mjs FILE

Use Astro’s official @astrojs/sitemap integration to automatically generate a sitemap for your site. This helps search engines discover your pages faster and understand your site structure.

Note: Because of Integration API limits, the sitemap integration can’t analyze a page’s source code. You can set site‑wide defaults or customize entries per page using the serialize option.


1️⃣ Install

Use the guided add command (recommended):

npx astro add sitemap

Or install manually:

npm i @astrojs/sitemap

2️⃣ Enable in astro.config

You must set your site URL and add sitemap() to integrations. Reference implementation:

import { defineConfig } from 'astro/config';
import netlify from '@astrojs/netlify';
import tailwindcss from '@tailwindcss/vite';
import alpinejs from '@astrojs/alpinejs';
import mdx from '@astrojs/mdx';
import react from '@astrojs/react';
import { fileURLToPath } from 'node:url';
import universalService from "./src/image/netlify-universal-service.ts";

import sitemap from '@astrojs/sitemap';

export default defineConfig({
  output: 'server',
  adapter: netlify({
    includedFiles: ['src/content/sales/**/*.{md,mdx}'],
  }),
  image: {
    service: universalService,
  },
  integrations: [alpinejs(), mdx(), react(), sitemap()],
  site: 'https://astromerchant.com',
  vite: {
    plugins: [tailwindcss()],
    define: {
      'import.meta.env.PUBLIC_SUPABASE_URL': JSON.stringify(process.env.SUPABASE_URL),
      'import.meta.env.PUBLIC_SUPABASE_ANON_KEY': JSON.stringify(process.env.SUPABASE_ANON_KEY),
    },
    resolve: {
      dedupe: ['react', 'react-dom'],
      alias: {
        '@data': fileURLToPath(new URL('./src/data', import.meta.url)),
      },
    },
  },
  viewTransitions: true,
});

Tip: Use the full origin (including https://) and no trailing slash for site.


3️⃣ Build & where to find it

After building/deploying, your sitemap is served automatically (commonly at /sitemap-index.xml). Submit it in Google Search Console and Bing Webmaster Tools for faster discovery.


4️⃣ Customize entries with serialize

Set site‑wide defaults and tweak specific routes (e.g., homepage priority, exclude admin pages) using the serialize option:

// astro.config.ts
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';

export default defineConfig({
  // ...
  integrations: [
    sitemap({
      // Return null to exclude a URL
      serialize: (item) => {
        const url = item.url.pathname;

        // Exclude private/admin/dashboard routes
        if (url.startsWith('/admin') || url.startsWith('/dashboard')) return null;

        // Example defaults
        const isHome = url === '/';
        return {
          ...item,
          changefreq: isHome ? 'daily' : 'weekly',
          priority: isHome ? 1.0 : 0.7,
          // Use a single timestamp for consistency, or derive from content dates.
          lastmod: new Date().toISOString(),
        };
      },
    }),
  ],
});

Common patterns:

  • Boost priority for / and key landing pages.
  • Lower (or exclude) /tags/, /search, /404, /admin, etc.
  • Derive lastmod from frontmatter dates in your collections if you store updated/modified fields.

5️⃣ Adding pages not in your routes (optional)

If you have canonical pages that aren’t part of Astro’s routed output (e.g., a headless asset URL or an external docs site), you can add them inside serialize by returning additional objects from elsewhere in your build process, or by generating those pages in your content pipeline first so the integration can see them. (Keeping everything as routed pages is typically simplest.)


✅ Quick checklist

  • @astrojs/sitemap installed
  • site set to your production URL
  • sitemap() added to integrations
  • serialize configured for priorities/exclusions
  • Sitemap submitted to search consoles (optional but recommended)

Free shipping over £60.00

Delivered with care. No code needed.