Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

fix(preload): tolerate a non-object speculationrules body

Rudy Celeklief510218c71a
diff --git a/scripts/preload_check.py b/scripts/preload_check.py
index 645aab9..03f6303 100644
--- a/scripts/preload_check.py
+++ b/scripts/preload_check.py
@@ -100,6 +100,10 @@ def _extract_speculation_actions(body: str) -> list[str]:
payload = json.loads(body.strip())
except json.JSONDecodeError:
return []
+ if not isinstance(payload, dict):
+ # A speculationrules body must be a JSON object; a bare array,
+ # string, or null is valid JSON but carries no action fields.
+ return []
for action_kind in ("prefetch", "prerender"):
if isinstance(payload.get(action_kind), list):
actions.add(action_kind)
diff --git a/tests/test_preload_check.py b/tests/test_preload_check.py
new file mode 100644
index 0000000..2c5c1af
--- /dev/null
+++ b/tests/test_preload_check.py
@@ -0,0 +1,43 @@
+"""
+Tests for scripts/preload_check.py — the Speculation Rules / preload auditor.
+
+Regression coverage for non-object speculationrules bodies: ``json.loads``
+accepts a bare array, string, or ``null`` (all valid JSON), so the
+``JSONDecodeError`` guard does not catch them. Before the fix,
+``_extract_speculation_actions`` then called ``.get()`` on a non-dict and
+raised ``AttributeError``, which propagated through ``analyse`` and crashed
+the whole audit on an otherwise-fetchable page.
+"""
+
+from __future__ import annotations
+
+import os
+import sys
+
+_SCRIPTS = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "scripts")
+if _SCRIPTS not in sys.path:
+ sys.path.insert(0, _SCRIPTS)
+
+import preload_check # noqa: E402
+
+
+def test_non_object_speculationrules_body_yields_no_actions() -> None:
+ # Valid JSON that is not an object: array, null, string, number.
+ for body in ('[{"source": "list", "urls": ["/next"]}]', "null", '"x"', "123"):
+ assert preload_check._extract_speculation_actions(body) == [], body
+
+
+def test_object_speculationrules_body_still_extracts_actions() -> None:
+ body = '{"prefetch": [{"source": "list", "urls": ["/a"]}]}'
+ assert preload_check._extract_speculation_actions(body) == ["prefetch"]
+
+
+def test_analyse_does_not_crash_on_array_speculationrules_block() -> None:
+ html = (
+ '<html><head><script type="speculationrules">'
+ '[{"source": "list", "urls": ["/next"]}]'
+ "</script></head><body></body></html>"
+ )
+ # Must not raise; the block contributes no actions.
+ result = preload_check.analyse(html)
+ assert result["speculation_rules"]["actions"] == []