From 9326d790de46e0557be4ea9bf8cf681d0868f140 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 22 May 2021 00:33:23 -0400 Subject: [PATCH] Server: redirect if server sends static path --- src/Ema/Server.hs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Ema/Server.hs b/src/Ema/Server.hs index 7d85dff..de43f93 100644 --- a/src/Ema/Server.hs +++ b/src/Ema/Server.hs @@ -71,8 +71,9 @@ runServerWithWebSocketHotReload port model render = do sendRouteHtmlToClient r s = do case renderWithEmaHtmlShims logger s r of Left staticPath -> - -- TODO - undefined + -- HACK: Websocket client should check for REDIRECT prefix. + -- Not bothering with JSON to avoid having to JSON parse every HTML dump. + liftIO $ WS.sendTextData conn $ "REDIRECT " <> toText staticPath Right html -> liftIO $ WS.sendTextData conn html log LevelDebug $ " ~~> " <> show r @@ -272,9 +273,13 @@ wsClientShim = ws.onmessage = evt => { console.log("ema: ✍ Patching DOM") - setHtml(document.documentElement, evt.data); - // reloadScripts(document.documentElement); - watchCurrentRoute(); + if evt.data.startsWith("REDIRECT ") { + document.location.href = evt.data.str.slice("REDIRECT ".length); + } else { + setHtml(document.documentElement, evt.data); + // reloadScripts(document.documentElement); + watchCurrentRoute(); + }; }; window.onbeforeunload = evt => { ws.close(); }; window.onpagehide = evt => { ws.close(); };