Skip to content

Commit a14c010

Browse files
soareschenclaude
andcommitted
Check the live site's key file before an IndexNow send
The engines verify a submission against the key file on the live site, so `--from` now changes only where the sitemap is read, and the key check always reads the live site. A dropped connection or a timeout during the send ends with a message instead of a traceback. The README says that `robots.txt` names the sitemap for every crawler. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5250175 commit a14c010

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ workflow also runs the `example-code` snippet tests.
7272

7373
## Search engines
7474

75-
Search engines learn about the site from its sitemap, which Docusaurus generates at `/sitemap.xml`,
76-
and Google reads it through Search Console. Bing and the engines that share its index can also be
77-
told about changed pages straight away through IndexNow, and `scripts/indexnow/submit.py` sends
78-
those notices.
75+
Search engines learn about the site from its sitemap, which Docusaurus generates at `/sitemap.xml`
76+
and `static/robots.txt` names for every crawler, and Google also reads it through Search Console.
77+
Bing and the engines that share its index can be told about changed pages straight away through
78+
IndexNow, and `scripts/indexnow/submit.py` sends those notices.
7979

8080
IndexNow checks that a submission comes from the site's owner by fetching a key file from the site.
8181
That file is `static/251047bc35e2154870673d415006ae6e.txt`, whose name and only content are the key.

‎scripts/indexnow/submit.py‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
`--all` submits every URL in the live sitemap and every redirect stub under `static/`, which suits
1313
only a relaunch or migration; after one, submit only the pages that changed. `--dry-run` prints the
1414
list and whether the live key file matches, without sending anything. `--from` reads the sitemap
15-
and the key file from another origin, such as `npm run serve`, and `--endpoint` sends to another
16-
IndexNow endpoint.
15+
from another origin, such as `npm run serve`, and `--endpoint` sends to another IndexNow endpoint.
1716
18-
It refuses a URL on another host, and refuses to send while the live key file is missing or differs
19-
from the one in `static/`. It exits 0 when the endpoint answers 200 or 202, and 1 otherwise.
17+
It refuses a URL on another host, and refuses to send while the key file on the live site is
18+
missing or differs from the one in `static/`, since the engines check the live site. It exits 0
19+
when the endpoint answers 200 or 202, and 1 otherwise.
2020
"""
2121
import argparse
2222
import json
@@ -62,10 +62,16 @@ def fetch(url):
6262
try:
6363
with urllib.request.urlopen(url, timeout=30) as response:
6464
return response.read().decode()
65-
except (urllib.error.URLError, TimeoutError):
65+
except (OSError, UnicodeDecodeError):
6666
return None
6767

6868

69+
def live_key_matches(key):
70+
"""Return whether the live site serves the key file the engines will check."""
71+
live = fetch(f"{SITE}/{key}.txt")
72+
return live is not None and live.strip() == key
73+
74+
6975
def stub_urls():
7076
"""Return the site URL of each redirect stub under `static/`."""
7177
urls = []
@@ -90,7 +96,7 @@ def main():
9096
parser.add_argument("urls", nargs="*", help="the changed pages' URLs")
9197
parser.add_argument("--all", action="store_true", help="every sitemap URL and redirect stub")
9298
parser.add_argument("--dry-run", action="store_true", help="print the list without sending")
93-
parser.add_argument("--from", dest="origin", default=SITE, help="where to read the sitemap and key")
99+
parser.add_argument("--from", dest="origin", default=SITE, help="where to read the sitemap")
94100
parser.add_argument("--endpoint", default=ENDPOINT, help="the IndexNow endpoint")
95101
args = parser.parse_args()
96102

@@ -99,14 +105,13 @@ def main():
99105
key = find_key()
100106
urls = all_urls(args.origin.rstrip("/")) if args.all else args.urls
101107

102-
foreign = [url for url in urls if urlsplit(url).scheme != "https" or urlsplit(url).netloc != HOST]
108+
foreign = [u for u in urls if urlsplit(u).scheme != "https" or urlsplit(u).netloc != HOST]
103109
if foreign:
104110
sys.exit("not on https://" + HOST + ":\n " + "\n ".join(foreign))
105111
if len(urls) > MAX_URLS:
106112
sys.exit(f"{len(urls)} URLs exceed IndexNow's limit of {MAX_URLS} per request")
107113

108-
live = fetch(f"{args.origin.rstrip('/')}/{key}.txt")
109-
key_ok = live is not None and live.strip() == key
114+
key_ok = live_key_matches(key)
110115
key_status = "matches" if key_ok else "is missing or differs from static/"
111116
count = f"{len(urls)} URL" if len(urls) == 1 else f"{len(urls)} URLs"
112117

@@ -131,8 +136,8 @@ def main():
131136
status = response.status
132137
except urllib.error.HTTPError as error:
133138
status = error.code
134-
except urllib.error.URLError as error:
135-
sys.exit(f"not sent: {error.reason}")
139+
except OSError as error:
140+
sys.exit(f"not sent: {getattr(error, 'reason', error)}")
136141
print(f"{count} sent to {args.endpoint}: {status} {MEANINGS.get(status, 'unexpected')}")
137142
return 0 if status in (200, 202) else 1
138143

0 commit comments

Comments
 (0)