Sub topic 10
Accurate lastmod values in sitemaps
This chapter assumes you can read basic XML and HTML and know what a crawler is. It also assumes you have met the idea of a single source of truth for each fact, and the difference between a published date and a last-modified date. If not, review those chapters first.
What lastmod is for
A sitemap is an XML file that lists the URLs you want crawlers to know about. Each <url> entry can carry a <lastmod> element. It tells a crawler when the page last changed in a meaningful way.
Crawlers have a limited budget for fetching your pages. When lastmod is trustworthy, a crawler can skip pages that have not changed and revisit pages that have. A price change, a stock change, or a corrected specification then reaches search indexes and AI answer systems sooner.
The value is only useful if it is honest. Search engines have said publicly that they use lastmod only when it has proven reliable across a site. If every URL claims to have changed today, crawlers learn to ignore the field. Your genuine updates then get no benefit.
The other sitemap fields matter less. Google has said it ignores <priority> and <changefreq>. Spend your effort on lastmod.
The format
The value is a date or a date with time, in W3C Datetime format, which is a profile of ISO 8601. Both forms below are valid:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/p/trail-runner-2</loc>
<lastmod>2026-11-20</lastmod>
</url>
<url>
<loc>https://example.com/guides/trail-shoes</loc>
<lastmod>2026-11-20T08:00:00+00:00</lastmod>
</url>
</urlset>Include a time zone offset when you include a time. A timestamp without one is ambiguous, and different consumers may read it differently. A date alone, such as 2026-11-20, is acceptable for pages that change at most daily.
Which date belongs in lastmod
Use the date of the last meaningful change to the page. This is the same idea as the dateModified value in your Article JSON-LD. In fact, the two should come from the same stored field. If the JSON-LD says the guide was modified on 2026-11-20 and the sitemap says 2026-11-28, one of them is wrong, and a crawler can see both.
A change is meaningful when a reader or an answer system would get different information. Examples:
- A price, currency, or availability change.
- A corrected specification, such as a weight or a size chart.
- A rewritten paragraph, a new section, or a removed claim.
- A product moved to discontinued status.
These changes are not meaningful:
- A new copyright year in the footer.
- A rotating "recommended products" widget.
- A visitor counter, a timestamp showing the current time, or a randomised banner.
- A rebuild of the site that regenerates every file with identical content.
The last item is the most common cause of bad lastmod values. Many static site generators and CMS plugins use the file's modification time, or the build time. Every deploy then marks every page as changed.
Common ways lastmod goes wrong
| Problem | Cause | Effect |
|---|---|---|
| Every URL has today's date | Value generated at request time or build time | Crawlers stop trusting the field |
| Date never changes | Value set once at creation | Real updates are not recrawled promptly |
| Date differs from `dateModified` | Two separate fields, edited by hand | Conflicting signals on the same page |
| Date changes on boilerplate edits | Template or widget changes touch every page | Noise across the whole site |
| Future date | Timezone mistake or scheduled content | Entry may be ignored |
| Sitemap not regenerated | Cron job failed or output is cached | Old values served after real changes |
Deriving lastmod from the source of truth
The reliable fix follows the pattern used throughout this tutorial. Store the modification timestamp once, on the record that owns the content. Then render it into every place it appears.
Product facts are a good example. A product record has a facts_updated_at field. Your code sets it only when a customer-visible field changes: price, currency, stock status, name, or description. It must not be set when an internal field changes, such as a warehouse bin location or a stock count that does not alter the in-stock status.
from datetime import datetime, timezone
from xml.sax.saxutils import escape
# Fields whose change is visible to the reader or to an answer system.
VISIBLE_FIELDS = {"name", "description", "price", "currency", "in_stock"}
def apply_update(record, changes, now=None):
"""Apply changes and bump facts_updated_at only for visible fields."""
now = now or datetime.now(timezone.utc)
changed_visible = False
for field, value in changes.items():
if record.get(field) != value:
record[field] = value
if field in VISIBLE_FIELDS:
changed_visible = True
if changed_visible:
record["facts_updated_at"] = now
return record
def sitemap_xml(records):
lines = [
'<?xml version="1.0" encoding="UTF-8"?>',
'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">',
]
for r in records:
stamp = r["facts_updated_at"].isoformat(timespec="seconds")
lines.append(" <url>")
lines.append(" <loc>" + escape(r["url"]) + "</loc>")
lines.append(" <lastmod>" + stamp + "</lastmod>")
lines.append(" </url>")
lines.append("</urlset>")
return "\n".join(lines)
if __name__ == "__main__":
created = datetime(2026, 3, 2, 9, 0, tzinfo=timezone.utc)
product = {
"url": "https://example.com/p/trail-runner-2",
"name": "Trail Runner 2",
"description": "Light trail shoe.",
"price": "89.00",
"currency": "EUR",
"in_stock": True,
"warehouse_bin": "A-14",
"facts_updated_at": created,
}
# Internal change: lastmod stays at the creation timestamp.
apply_update(product, {"warehouse_bin": "B-02"})
print(sitemap_xml([product]))
# Visible change: lastmod moves to the supplied timestamp.
later = datetime(2026, 11, 20, 8, 0, tzinfo=timezone.utc)
apply_update(product, {"price": "79.00"}, now=later)
print(sitemap_xml([product]))The first printed sitemap carries 2026-03-02T09:00:00+00:00. The second carries 2026-11-20T08:00:00+00:00. The same facts_updated_at value should feed dateModified in JSON-LD, so the sitemap and the markup cannot disagree.
For a page that combines several records, such as a category page listing twenty products, use the most recent facts_updated_at among the facts it displays, plus the page's own editorial change date. Take the maximum. Ignore sort order or pagination changes unless you consider them meaningful.
When the sitemap itself changes
The sitemap file also has its own freshness. Keep these points in mind:
- Regenerate it whenever a visible fact changes, or on a short schedule such as every few minutes for a busy store. A nightly job means a price change can sit unannounced for up to a day.
- Do not serve a stale cached copy of the sitemap. Apply the same cache rules you use for pages, covered in the HTTP caching chapters.
- A single sitemap file may hold at most 50,000 URLs and be at most 50 MB uncompressed. Larger sites split into several files and list them in a sitemap index.
- In a sitemap index, each
<sitemap>entry may also have a<lastmod>. Set it to the latestlastmodinside that child file, not to the time you generated it. - List only canonical, indexable URLs that return a 200 status. Redirected, blocked, or
noindexURLs make the file less reliable.
A sitemap index with honest values looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<sitemap>
<loc>https://example.com/sitemap-products-1.xml</loc>
<lastmod>2026-11-20T08:00:00+00:00</lastmod>
</sitemap>
<sitemap>
<loc>https://example.com/sitemap-guides.xml</loc>
<lastmod>2026-11-12T15:30:00+00:00</lastmod>
</sitemap>
</sitemapindex>Discontinued and out-of-stock products
When a product is discontinued, the page changes in a way that matters. Set facts_updated_at, so lastmod moves, and update the page. What happens next depends on your policy for retired products:
- If the page stays up with a "no longer available" notice and alternatives, keep it in the sitemap with the new
lastmod. - If the page returns a 404 or 410 status, or redirects to a replacement, remove it from the sitemap. Leaving dead URLs in the file wastes crawl budget and lowers trust in the whole list.
A temporary out-of-stock state should still move lastmod, because the availability value in your Offer markup changed.
Checking your sitemap
Spot-check a sample from the live file. Fetch a URL from the sitemap, read its dateModified and visible content, and compare. Then look for patterns that indicate gaming or a bug:
- Does more than a small share of URLs share exactly the same
lastmodvalue? - Are any values later than the current time?
- Does the value change when you redeploy without editing content?
Automated versions of these checks belong in the monitoring chapter. The core test is simple: if you can redeploy the site and every lastmod changes, the field is not carrying information.
Exercise
A store's sitemap generator uses the build time as lastmod. The site deploys twice a day, and only about 40 of 12,000 product pages change in a given week. Describe what goes wrong and how you would fix it.
Show answer
Every deploy gives all 12,000 URLs a new lastmod, so the field claims constant change on every page. Crawlers learn that the value is unreliable and ignore it, which means the 40 pages with real changes get no faster recrawl. The fix is to store a facts_updated_at timestamp on each product record, update it only when a visible field (name, description, price, currency, stock status) changes, and generate both the sitemap lastmod and the JSON-LD dateModified from that field. Optionally add a content hash so that regenerated pages with identical facts keep their old timestamp.
Exercise
A product page's JSON-LD says dateModified is 2026-11-20, but its sitemap entry says lastmod is 2026-11-28. A developer says the sitemap value is newer, so it is fine. What is wrong with that view, and what should you check?
Show answer
The two values describe the same event, the last meaningful change, and they should come from one stored field. A mismatch means that at least one is generated differently, for example from a file modification time or a separate manually edited field. Crawlers and answer systems see both and may trust neither. Check what generates each value, find out whether anything visible changed on 2026-11-28, and then make both read from the same facts_updated_at field.