Urgent.News

What's breaking now, across hundreds of outlets.

Tech

Your sitemap lastmod Is Lying. Here's a 40-Line Fix That Made Mine Honest

If your site is built from templates, there is a good chance every page in your sitemap has the same lastmod date — the date of your last deploy. That is not a lie you tell on purpose. It falls out of the build: a footer change touches all 1,287 files, the generator stamps each one with now() , and the sitemap faithfully reports that the entire site changed at 14:32 on Tuesday. To a crawler, that…

Your site's sitemap likely displays the same lastmod date for every page, which is not an intentional lie. This information is generated automatically by your build system, and the sitemap reflects that the entire site changed at a specific time. However, this can mislead search crawlers, causing them to treat all pages equally and potentially delaying indexing of important pages.

In your specific case, 51% of your crawl budget was allocated to refreshing the entire site, while 49% was spent on discovering pages that were not indexed. To address this issue, you need to implement a content hash system for your lastmod attribute. This approach focuses on whether the content of a specific page has changed, rather than whether the underlying template or build process has altered.

To achieve this, you should extract the meaningful body of each page, hash it, and store the hash value. Only update the lastmod attribute when the hash value differs. This method ensures that only truly modified pages are flagged for crawling and indexing.

The process involves creating a function that reads the HTML content of a page, removes shared blocks such as the navigation menu, footer, and call-to-action band, collapses whitespace for consistency, and finally computes a SHA256 hash of the remaining content. This hash serves as a fingerprint of the page's content.

A state file is maintained, containing each page's hash and corresponding lastmod date. The update step involves iterating over all HTML files in your site, calculating the hash for each page's body, and comparing it with the stored hash. If the hashes differ, it indicates that the content has changed, and the lastmod value should be updated accordingly.

By implementing this solution, your sitemap will accurately reflect the freshness of your content, allowing search crawlers to prioritize indexing of the most relevant pages. This should result in more efficient use of your crawl budget, as well as improved visibility for the pages that actually matter to your users.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

More from Tuesday 24 November →