sx-http: fix navigation + Playwright nav tests — 5/5 pass
AJAX navigation: detect SX-Request/HX-Request headers and return just the #main-panel fragment instead of the full page shell. Fixes layout break where header and content appeared side-by-side after navigation. New navigation test suite (tests/playwright/navigation.spec.js): - layout stays vertical after clicking nav link - content updates after navigation - no raw SX component calls visible after navigation - header island survives navigation - full page width is used (no side-by-side split) All 5 tests pass. 14 total Playwright tests pass. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -2052,12 +2052,26 @@ let http_mode port =
|
||||
(try Unix.close client with _ -> ())
|
||||
in
|
||||
|
||||
(* Check if request has SX-Request or HX-Request header (AJAX navigation) *)
|
||||
let is_sx_request data =
|
||||
let lower = String.lowercase_ascii data in
|
||||
let has_substring s sub =
|
||||
let slen = String.length s and sublen = String.length sub in
|
||||
if sublen > slen then false
|
||||
else let rec check i = if i > slen - sublen then false
|
||||
else if String.sub s i sublen = sub then true else check (i + 1)
|
||||
in check 0
|
||||
in
|
||||
has_substring lower "sx-request" || has_substring lower "hx-request"
|
||||
in
|
||||
|
||||
(* Handle one HTTP request *)
|
||||
let handle_client env client =
|
||||
let buf = Bytes.create 8192 in
|
||||
let n = try Unix.read client buf 0 8192 with _ -> 0 in
|
||||
if n > 0 then begin
|
||||
let data = Bytes.sub_string buf 0 n in
|
||||
let is_ajax = is_sx_request data in
|
||||
let response =
|
||||
try
|
||||
match parse_http_request data with
|
||||
@@ -2072,18 +2086,50 @@ let http_mode port =
|
||||
else
|
||||
let is_sx = path = "/sx/" || path = "/sx"
|
||||
|| (String.length path > 4 && String.sub path 0 4 = "/sx/") in
|
||||
if is_sx then
|
||||
(* Check cache first *)
|
||||
if is_sx then begin
|
||||
if is_ajax then
|
||||
(* AJAX navigation — return just the content fragment,
|
||||
not the full page shell. The client swaps #main-panel. *)
|
||||
(match http_render_page env path with
|
||||
| Some html ->
|
||||
(* Extract #main-panel from the full page HTML *)
|
||||
let panel_start = try
|
||||
let idx = ref 0 in
|
||||
let found = ref false in
|
||||
while not !found && !idx < String.length html - 20 do
|
||||
if String.sub html !idx 18 = "id=\"main-panel\"" then
|
||||
found := true
|
||||
else
|
||||
idx := !idx + 1
|
||||
done;
|
||||
if !found then begin
|
||||
(* Walk back to find the opening < *)
|
||||
let start = ref !idx in
|
||||
while !start > 0 && html.[!start] <> '<' do
|
||||
start := !start - 1
|
||||
done;
|
||||
Some !start
|
||||
end else None
|
||||
with _ -> None in
|
||||
(match panel_start with
|
||||
| Some start ->
|
||||
(* Find matching close tag — scan for </section> or end *)
|
||||
let fragment = String.sub html start (String.length html - start) in
|
||||
http_response ~content_type:"text/html; charset=utf-8" fragment
|
||||
| None -> http_response html)
|
||||
| None -> http_response ~status:404 "<h1>Not Found</h1>")
|
||||
else
|
||||
(* Full page request — check cache *)
|
||||
match Hashtbl.find_opt response_cache path with
|
||||
| Some cached -> cached
|
||||
| None ->
|
||||
(* Cache miss — render, cache, return *)
|
||||
(match http_render_page env path with
|
||||
| Some html ->
|
||||
let resp = http_response html in
|
||||
Hashtbl.replace response_cache path resp;
|
||||
resp
|
||||
| None -> http_response ~status:404 "<h1>Not Found</h1>")
|
||||
end
|
||||
else if String.length path > 8 && String.sub path 0 8 = "/static/" then
|
||||
serve_static_file static_dir path
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user