Server: defhandler endpoints return HTML for HX-Request, SX for SX-Request

The handler dispatch (api.* paths) now checks for HX-Request header.
If present, the SX aser output is rendered to HTML via sx_render_to_html
before sending. SX-Request (from SX client navigation) still gets SX
wire format. This makes hx-* attributes work like real htmx — the
server returns HTML fragments that htmx can swap into the DOM.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-18 08:01:29 +00:00
parent d4f74b5b02
commit 9e0de8831f

View File

@@ -4000,7 +4000,17 @@ let http_mode port =
let body_str = match Sx_ref.eval_expr aser_call (Env env) with
| String s | SxExpr s -> s | v -> Sx_types.inspect v in
let status = !_pending_response_status in
http_response ~status ~content_type:"text/sx; charset=utf-8" body_str)
(* HX-Request → render to HTML for htmx; otherwise SX wire format *)
let is_hx = has_substring (String.lowercase_ascii data) "hx-request" in
if is_hx then
let html = try
let exprs = Sx_parser.parse_all body_str in
let expr = match exprs with [e] -> e | [] -> Nil | _ -> List (Symbol "<>" :: exprs) in
Sx_render.sx_render_to_html env expr env
with _ -> body_str in
http_response ~status ~content_type:"text/html; charset=utf-8" html
else
http_response ~status ~content_type:"text/sx; charset=utf-8" body_str)
with e ->
Printf.eprintf "[handler] Error for %s: %s\n%!" path (Printexc.to_string e);
http_response ~status:500 ~content_type:"text/sx; charset=utf-8"