Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions crawl4ai/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2920,11 +2920,15 @@ def compute_head_fingerprint(head_html: str) -> str:
if not head_html:
return ""

head_lower = head_html.lower()
signals = []

# Match tags/attributes case-insensitively, but extract values from the
# ORIGINAL head: lowercasing the whole head first would fold a case-only
# title/meta change (e.g. "iPhone" -> "IPHONE") to the same fingerprint, so
# the cache validator would treat a genuinely changed page as unchanged.

# Extract title
title_match = re.search(r'<title[^>]*>(.*?)</title>', head_lower, re.DOTALL)
title_match = re.search(r'<title[^>]*>(.*?)</title>', head_html, re.DOTALL | re.IGNORECASE)
if title_match:
signals.append(title_match.group(1).strip())

Expand All @@ -2946,7 +2950,7 @@ def compute_head_fingerprint(head_html: str) -> str:
rf'<meta[^>]*content=["\']([^"\']*)["\'][^>]*{attr_type}=["\']{re.escape(attr_value)}["\']',
]
for pattern in patterns:
match = re.search(pattern, head_lower)
match = re.search(pattern, head_html, re.IGNORECASE)
if match:
signals.append(match.group(1).strip())
break # Found this tag, move to next
Expand Down
23 changes: 23 additions & 0 deletions tests/cache_validation/test_head_fingerprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,26 @@ def test_real_world_head(self):
assert fp != ""
# Should be deterministic
assert fp == compute_head_fingerprint(head)

def test_value_case_change_changes_fingerprint(self):
"""A case-only change in a title/meta *value* must change the
fingerprint, otherwise the cache validator treats a genuinely updated
page as unchanged and serves stale content. Regression."""
assert compute_head_fingerprint(
"<head><title>iPhone</title></head>"
) != compute_head_fingerprint("<head><title>IPHONE</title></head>")
assert compute_head_fingerprint(
'<head><meta name="description" content="Buy Now"></head>'
) != compute_head_fingerprint(
'<head><meta name="description" content="BUY NOW"></head>'
)

def test_tag_and_attribute_case_does_not_change_fingerprint(self):
"""Tag/attribute case is still matched case-insensitively; only the
markup case (not the values) differing yields the same fingerprint."""
assert compute_head_fingerprint(
"<HEAD><TITLE>Hello</TITLE></HEAD>"
) == compute_head_fingerprint("<head><title>Hello</title></head>")
assert compute_head_fingerprint(
'<head><META NAME="description" CONTENT="Hi"></head>'
) == compute_head_fingerprint('<head><meta name="description" content="Hi"></head>')