From 9e0de8831f60b20675aa2a3dc15d9e88057930cb Mon Sep 17 00:00:00 2001 From: giles Date: Sat, 18 Apr 2026 08:01:29 +0000 Subject: [PATCH] Server: defhandler endpoints return HTML for HX-Request, SX for SX-Request MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- hosts/ocaml/bin/sx_server.ml | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hosts/ocaml/bin/sx_server.ml b/hosts/ocaml/bin/sx_server.ml index 93540f20..9466cff5 100644 --- a/hosts/ocaml/bin/sx_server.ml +++ b/hosts/ocaml/bin/sx_server.ml @@ -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"