🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
SEO••By Komal Nakrani

Add lastmod to the Astro Sitemap from Real Dates

Add accurate Astro sitemap lastmod dates: per-URL dates in serialize from frontmatter or Git history, why a global lastmod is a bad idea, and CI fetch depth.

#Astro#SEO#Sitemap#lastmod

To add lastmod to the Astro sitemap, use the serialize option of @astrojs/sitemap and set item.lastmod for each URL from a real date: the post’s updatedDate frontmatter, or the last Git commit that touched the page’s source file. Avoid the global lastmod option for real sites, because it stamps every URL with the same date and search engines learn to ignore it.

Key Takeaways

  • serialize(item) runs once per URL; set item.lastmod to an ISO date string or Date.
  • Frontmatter dates are the most accurate for content; Git dates cover everything else.
  • In CI, check out full history (fetch-depth: 0) or every Git date is the build commit.
  • Google uses lastmod when it is consistently accurate and ignores changefreq and priority.

Why lastmod should be per URL

Search engines use lastmod to decide which pages to recrawl first. If every URL shows the build date, nothing stands out, and the signal becomes noise. The integration does accept a single date:

sitemap({ lastmod: new Date() }) // same date on every URL: avoid

That is only reasonable for a site that is fully regenerated with new content each build. For everything else, compute the date per page.

Option 1: dates from frontmatter

If your posts have pubDate and updatedDate, read them in the config file. The config cannot use astro:content, so read the Markdown files directly with gray-matter:

// astro.config.mjs
import { defineConfig } from 'astro/config';
import sitemap from '@astrojs/sitemap';
import { readdirSync, readFileSync } from 'node:fs';
import matter from 'gray-matter';

const dir = './src/content/blog';
const dates = new Map(
  readdirSync(dir)
    .filter((f) => /\.mdx?$/.test(f))
    .map((f) => {
      const { data } = matter(readFileSync(`${dir}/${f}`, 'utf8'));
      return [`/blog/${f.replace(/\.mdx?$/, '')}/`, data.updatedDate ?? data.pubDate];
    })
);

export default defineConfig({
  site: 'https://www.example.com',
  integrations: [
    sitemap({
      serialize(item) {
        const date = dates.get(new URL(item.url).pathname);
        if (date) item.lastmod = new Date(date).toISOString();
        return item;
      },
    }),
  ],
});

Install gray-matter with npm install -D gray-matter. Pages without a match simply have no lastmod, which is better than a wrong one. Set updatedDate only for meaningful edits, not typo fixes.

Option 2: dates from Git history

For pages that are not content entries, such as src/pages/pricing.astro, the last commit date of the source file is a good proxy:

// sitemap-dates.mjs
import { execFileSync } from 'node:child_process';
import { existsSync } from 'node:fs';

export function gitDate(file) {
  if (!existsSync(file)) return undefined;
  const out = execFileSync('git', ['log', '-1', '--format=%cI', '--', file], {
    encoding: 'utf8',
  }).trim();
  return out || undefined;
}

export function sourceFor(pathname) {
  const clean = pathname.replace(/^\/|\/$/g, '') || 'index';
  return [`src/pages/${clean}.astro`, `src/pages/${clean}/index.astro`].find(existsSync);
}
// astro.config.mjs (sitemap excerpt)
import { gitDate, sourceFor } from './sitemap-dates.mjs';

sitemap({
  serialize(item) {
    const file = sourceFor(new URL(item.url).pathname);
    const date = file && gitDate(file);
    if (date) item.lastmod = date;
    return item;
  },
});

%cI prints the committer date in ISO 8601, which sitemaps accept as is. You can combine both options: try the frontmatter map first, then fall back to Git.

Make Git dates work in CI

Most CI systems clone with a depth of one commit, so every file appears to have changed in the latest commit. Fetch the full history in GitHub Actions:

# .github/workflows/deploy.yml (excerpt)
- uses: actions/checkout@v4
  with:
    fetch-depth: 0

Cloudflare Pages and Netlify builds also use shallow clones by default; check your host’s settings, or build in CI and deploy the dist/ folder. Our GitHub Actions guide has a full deploy workflow.

Check the output

After npm run build, open dist/sitemap-0.xml. Each <url> should have a <lastmod> only where you have a real date. Then submit the sitemap in Search Console; the Search Console tip walks through it. The base setup is in the sitemap tip, and the config docs show where the integration sits in a theme from our catalog.

FAQ

Does Google use lastmod in sitemaps?

Yes, when it is consistently accurate. Google has said it ignores changefreq and priority, and that it trusts lastmod on sites where the dates match real content changes.

What date format does sitemap lastmod need?

W3C Datetime, a subset of ISO 8601. 2026-11-02 and 2026-11-02T10:30:00Z are both valid. Date.prototype.toISOString() and Git’s %cI both produce valid values.

Why do all my lastmod dates show the same day?

Either you set the global lastmod option, or your CI made a shallow clone so every file’s last commit is the build commit. Use per-URL dates and fetch-depth: 0.

Should lastmod change when I edit the layout?

Only if the change is meaningful to the page’s main content. Updating every page’s date for a footer change dilutes the signal, which is why frontmatter dates are often better than Git dates for posts.