|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Verify the built VitePress sitemap still describes the site exactly: one entry |
| 4 | +# per published page, nothing extra, every entry dated, no date in the future. |
| 5 | +# Google only keeps using <lastmod> for recrawl scheduling while it stays |
| 6 | +# verifiably accurate, and a single leaked non-page (public/*.md has slipped in |
| 7 | +# before) is enough to poison that. |
| 8 | +# |
| 9 | +# Usage: check_sitemap.sh [dist-dir] |
| 10 | + |
| 11 | +set -eu |
| 12 | + |
| 13 | +dist="${1:-docs/.vitepress/dist}" |
| 14 | +sitemap="$dist/sitemap.xml" |
| 15 | +robots="$dist/robots.txt" |
| 16 | +site="https://bashunit.com" |
| 17 | +errors=0 |
| 18 | + |
| 19 | +fail() { |
| 20 | + printf 'sitemap: %s\n' "$1" >&2 |
| 21 | + errors=$((errors + 1)) |
| 22 | +} |
| 23 | + |
| 24 | +if [ ! -s "$sitemap" ]; then |
| 25 | + fail "$sitemap is missing or empty" |
| 26 | + exit 1 |
| 27 | +fi |
| 28 | + |
| 29 | +# 404 is the only built page that must stay out of the sitemap. |
| 30 | +built=$(cd "$dist" && find . -name '*.html' \ |
| 31 | + | sed 's|^\./||; s|\.html$||; s|^index$||; s|/index$|/|' \ |
| 32 | + | grep -v '^404$' | sort) |
| 33 | + |
| 34 | +locs=$(grep -o '<loc>[^<]*</loc>' "$sitemap" | sed 's|<loc>||; s|</loc>||') |
| 35 | +listed=$(printf '%s\n' "$locs" | sed "s|^$site/||" | sort) |
| 36 | + |
| 37 | +missing=$(comm -23 <(printf '%s\n' "$built") <(printf '%s\n' "$listed")) |
| 38 | +[ -z "$missing" ] || fail "pages built but not listed: $(echo "$missing" | tr '\n' ' ')" |
| 39 | + |
| 40 | +orphan=$(comm -13 <(printf '%s\n' "$built") <(printf '%s\n' "$listed")) |
| 41 | +[ -z "$orphan" ] || fail "listed but not built: $(echo "$orphan" | tr '\n' ' ')" |
| 42 | + |
| 43 | +foreign=$(printf '%s\n' "$locs" | grep -v "^$site/" || true) |
| 44 | +[ -z "$foreign" ] || fail "loc outside $site: $(echo "$foreign" | tr '\n' ' ')" |
| 45 | + |
| 46 | +# VitePress emits the whole sitemap on one line, so counting needs -o, not -c. |
| 47 | +urls=$(grep -o '<url>' "$sitemap" | wc -l | tr -d ' ') |
| 48 | +dated=$(grep -o '<lastmod>' "$sitemap" | wc -l | tr -d ' ') |
| 49 | +[ "$urls" = "$dated" ] || fail "$urls urls but $dated lastmod entries" |
| 50 | + |
| 51 | +future=$(grep -o '<lastmod>[^<]*</lastmod>' "$sitemap" \ |
| 52 | + | sed 's|<lastmod>||; s|</lastmod>||' | cut -c1-10 | sort -u \ |
| 53 | + | awk -v today="$(date -u +%Y-%m-%d)" '$0 > today' || true) |
| 54 | +[ -z "$future" ] || fail "lastmod in the future: $(echo "$future" | tr '\n' ' ')" |
| 55 | + |
| 56 | +if [ ! -f "$robots" ]; then |
| 57 | + fail "robots.txt is missing from $dist" |
| 58 | +elif ! grep -q "^Sitemap: $site/sitemap.xml\$" "$robots"; then |
| 59 | + fail "robots.txt does not advertise $site/sitemap.xml" |
| 60 | +fi |
| 61 | + |
| 62 | +if [ "$errors" -gt 0 ]; then |
| 63 | + exit 1 |
| 64 | +fi |
| 65 | + |
| 66 | +printf 'sitemap: OK (%s urls, all dated, robots.txt advertises it)\n' "$urls" |
0 commit comments