Skip to content

Commit 9af8fe1

Browse files
committed
fixes #789
1 parent ea029c5 commit 9af8fe1

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

fasthtml/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
'fasthtml.core._add_ids': ('api/core.html#_add_ids', 'fasthtml/core.py'),
8181
'fasthtml.core._annotations': ('api/core.html#_annotations', 'fasthtml/core.py'),
8282
'fasthtml.core._apply_ft': ('api/core.html#_apply_ft', 'fasthtml/core.py'),
83+
'fasthtml.core._canonical': ('api/core.html#_canonical', 'fasthtml/core.py'),
8384
'fasthtml.core._find_p': ('api/core.html#_find_p', 'fasthtml/core.py'),
8485
'fasthtml.core._find_targets': ('api/core.html#_find_targets', 'fasthtml/core.py'),
8586
'fasthtml.core._find_wsp': ('api/core.html#_find_wsp', 'fasthtml/core.py'),

fasthtml/core.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,19 @@ def _part_resp(req, resp):
421421
return resp,kw
422422

423423
# %% ../nbs/api/00_core.ipynb
424+
def _canonical(req):
425+
if not req.app.canonical: return []
426+
url = str(getattr(req, 'canonical', req.url)).replace('http://', 'https://', 1)
427+
return [Link(rel="canonical", href=url)]
428+
424429
def _xt_cts(req, resp):
425430
"Extract content and headers, render as full page or fragment"
426431
hdr_tags = 'title','meta','link','style','base'
427432
resp = tuplify(resp)
428433
heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)
429434
if not is_full_page(req, resp):
430435
title = [] if any(getattr(o, 'tag', '')=='title' for o in heads) else [Title(req.app.title)]
431-
canonical = [Link(rel="canonical", href=getattr(req, 'canonical', req.url))] if req.app.canonical else []
432-
resp = respond(req, [*heads, *title, *canonical], bdy)
436+
resp = respond(req, [*heads, *title, *_canonical(req)], bdy)
433437
return _to_xml(req, resp, indent=fh_cfg.indent)
434438

435439
# %% ../nbs/api/00_core.ipynb
@@ -481,12 +485,13 @@ async def _wrap_call(f, req, params):
481485
"multi-swap": "https://cdn.jsdelivr.net/npm/[email protected]/multi-swap.js",
482486
"path-deps": "https://cdn.jsdelivr.net/npm/[email protected]/path-deps.js",
483487
"remove-me": "https://cdn.jsdelivr.net/npm/[email protected]/remove-me.js",
488+
"debug": "https://unpkg.com/[email protected]/dist/ext/debug.js",
484489
"ws": "https://cdn.jsdelivr.net/npm/[email protected]/ws.js",
485490
"chunked-transfer": "https://cdn.jsdelivr.net/npm/[email protected]/transfer-encoding-chunked.js"
486491
}
487492

488493
# %% ../nbs/api/00_core.ipynb
489-
htmxsrc = Script(src="https://cdn.jsdelivr.net/npm/[email protected].6/dist/htmx.min.js")
494+
htmxsrc = Script(src="https://cdn.jsdelivr.net/npm/[email protected].7/dist/htmx.js")
490495
fhjsscr = Script(src="https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js")
491496
surrsrc = Script(src="https://cdn.jsdelivr.net/gh/answerdotai/surreal@main/surreal.js")
492497
scopesrc = Script(src="https://cdn.jsdelivr.net/gh/gnat/css-scope-inline@main/script.js")

nbs/api/00_core.ipynb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
{
132132
"data": {
133133
"text/plain": [
134-
"datetime.datetime(2025, 10, 5, 14, 0)"
134+
"datetime.datetime(2025, 10, 27, 14, 0)"
135135
]
136136
},
137137
"execution_count": null,
@@ -1322,15 +1322,19 @@
13221322
"outputs": [],
13231323
"source": [
13241324
"#| export\n",
1325+
"def _canonical(req):\n",
1326+
" if not req.app.canonical: return []\n",
1327+
" url = str(getattr(req, 'canonical', req.url)).replace('http://', 'https://', 1)\n",
1328+
" return [Link(rel=\"canonical\", href=url)]\n",
1329+
"\n",
13251330
"def _xt_cts(req, resp):\n",
13261331
" \"Extract content and headers, render as full page or fragment\"\n",
13271332
" hdr_tags = 'title','meta','link','style','base'\n",
13281333
" resp = tuplify(resp)\n",
13291334
" heads,bdy = partition(resp, lambda o: getattr(o, 'tag', '') in hdr_tags)\n",
13301335
" if not is_full_page(req, resp):\n",
13311336
" title = [] if any(getattr(o, 'tag', '')=='title' for o in heads) else [Title(req.app.title)]\n",
1332-
" canonical = [Link(rel=\"canonical\", href=getattr(req, 'canonical', req.url))] if req.app.canonical else []\n",
1333-
" resp = respond(req, [*heads, *title, *canonical], bdy)\n",
1337+
" resp = respond(req, [*heads, *title, *_canonical(req)], bdy)\n",
13341338
" return _to_xml(req, resp, indent=fh_cfg.indent)"
13351339
]
13361340
},
@@ -1422,6 +1426,7 @@
14221426
" \"multi-swap\": \"https://cdn.jsdelivr.net/npm/[email protected]/multi-swap.js\",\n",
14231427
" \"path-deps\": \"https://cdn.jsdelivr.net/npm/[email protected]/path-deps.js\",\n",
14241428
" \"remove-me\": \"https://cdn.jsdelivr.net/npm/[email protected]/remove-me.js\",\n",
1429+
" \"debug\": \"https://unpkg.com/[email protected]/dist/ext/debug.js\",\n",
14251430
" \"ws\": \"https://cdn.jsdelivr.net/npm/[email protected]/ws.js\",\n",
14261431
" \"chunked-transfer\": \"https://cdn.jsdelivr.net/npm/[email protected]/transfer-encoding-chunked.js\"\n",
14271432
"}"
@@ -1445,7 +1450,7 @@
14451450
"name": "stdout",
14461451
"output_type": "stream",
14471452
"text": [
1448-
"morph head-support preload class-tools loading-states multi-swap path-deps remove-me ws chunked-transfer\n"
1453+
"morph head-support preload class-tools loading-states multi-swap path-deps remove-me debug ws chunked-transfer\n"
14491454
]
14501455
}
14511456
],
@@ -1461,7 +1466,7 @@
14611466
"outputs": [],
14621467
"source": [
14631468
"#| export\n",
1464-
"htmxsrc = Script(src=\"https://cdn.jsdelivr.net/npm/[email protected].6/dist/htmx.min.js\")\n",
1469+
"htmxsrc = Script(src=\"https://cdn.jsdelivr.net/npm/[email protected].7/dist/htmx.js\")\n",
14651470
"fhjsscr = Script(src=\"https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js\")\n",
14661471
"surrsrc = Script(src=\"https://cdn.jsdelivr.net/gh/answerdotai/surreal@main/surreal.js\")\n",
14671472
"scopesrc = Script(src=\"https://cdn.jsdelivr.net/gh/gnat/css-scope-inline@main/script.js\")\n",
@@ -2879,13 +2884,13 @@
28792884
"name": "stdout",
28802885
"output_type": "stream",
28812886
"text": [
2882-
"Set to 2025-09-17 09:13:49.740165\n"
2887+
"Set to 2025-10-27 07:27:07.025956\n"
28832888
]
28842889
},
28852890
{
28862891
"data": {
28872892
"text/plain": [
2888-
"'Session time: 2025-09-17 09:13:49.740165'"
2893+
"'Session time: 2025-10-27 07:27:07.025956'"
28892894
]
28902895
},
28912896
"execution_count": null,
@@ -3482,10 +3487,10 @@
34823487
" <html>\n",
34833488
" <head>\n",
34843489
" <title>FastHTML page</title>\n",
3485-
" <link rel=\"canonical\" href=\"http://testserver/setcookie\">\n",
3490+
" <link rel=\"canonical\" href=\"https://testserver/setcookie\">\n",
34863491
" <meta charset=\"utf-8\">\n",
34873492
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, viewport-fit=cover\">\n",
3488-
"<script src=\"https://cdn.jsdelivr.net/npm/[email protected].6/dist/htmx.min.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/answerdotai/surreal@main/surreal.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/gnat/css-scope-inline@main/script.js\"></script><script>\n",
3493+
"<script src=\"https://cdn.jsdelivr.net/npm/[email protected].7/dist/htmx.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/answerdotai/[email protected]/fasthtml.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/answerdotai/surreal@main/surreal.js\"></script><script src=\"https://cdn.jsdelivr.net/gh/gnat/css-scope-inline@main/script.js\"></script><script>\n",
34893494
" function sendmsg() {\n",
34903495
" window.parent.postMessage({height: document.documentElement.offsetHeight}, '*');\n",
34913496
" }\n",
@@ -3502,7 +3507,7 @@
35023507
{
35033508
"data": {
35043509
"text/plain": [
3505-
"'Cookie was set at time 08:12:02.333734'"
3510+
"'Cookie was set at time 07:28:31.264910'"
35063511
]
35073512
},
35083513
"execution_count": null,

0 commit comments

Comments
 (0)