Skip to content

Commit ce68df1

Browse files
A1L13Naoclaude
authored
learn: fix page views never being counted (#1178)
On Workers, Response.headers is a plain dict and the Content-Type key is not stored as "Content-Type", so every page was treated as non-HTML and skipped. Match the header name case-insensitively. Co-authored-by: ao <ao@a.lan> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4072cd2 commit ce68df1

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

‎learn/src/worker.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6641,7 +6641,14 @@ async def record_page_view(request, env, response) -> None:
66416641
try:
66426642
if getattr(response, "status", 200) != 200:
66436643
return
6644-
ctype = (response.headers.get("Content-Type") or "").lower()
6644+
# On Workers, Response.headers is a plain dict whose key case varies.
6645+
headers = response.headers
6646+
pairs = headers.items() if hasattr(headers, "items") else headers
6647+
ctype = ""
6648+
for key, value in pairs:
6649+
if str(key).lower() == "content-type":
6650+
ctype = str(value or "").lower()
6651+
break
66456652
if "text/html" not in ctype:
66466653
return
66476654
path = _page_view_path(request, env)

‎learn/tests/test_page_views.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,15 @@ async def test_counts_html_200(self):
6464
assert args[1] == "/activity/react"
6565
stmt.bind.return_value.run.assert_awaited()
6666

67+
async def test_content_type_key_is_case_insensitive(self):
68+
stmt = make_stmt()
69+
db = MagicMock()
70+
db.prepare = MagicMock(return_value=stmt)
71+
env = make_env(db=db)
72+
resp = SimpleNamespace(status=200, headers={"content-type": "text/html; charset=utf-8"})
73+
await worker.record_page_view(_req("/about"), env, resp)
74+
assert stmt.bind.call_args[0][1] == "/about"
75+
6776
async def test_ignores_errors_and_non_html(self):
6877
db = MagicMock()
6978
db.prepare = MagicMock()

0 commit comments

Comments
 (0)