on master help sign in
Render jsaddle page on GET 404 (for client side routing) (fixes #34)
commitb22d820561f9b1d3c58f77291a9a2f6b8551cab6 authorTom Smalley <tfsmalley@gmail.com> dateSat 6 Jan 2018, 13:36 (9 years ago) parentc3b5d55
1 file changed+11−3
jsaddle-warp/src/Language/Javascript/JSaddle/WebSockets.hs+11−3
jsaddle-warp/src/Language/Javascript/JSaddle/WebSockets.hs+11−3
@@ -40,10 +40,11 @@ import Network.WebSockets
4040 receiveDataMessage, acceptRequest, ServerApp, sendPing)
4141import qualified Network.WebSockets as WS (DataMessage(..))
4242import Network.Wai.Handler.WebSockets (websocketsOr)
43+import Network.HTTP.Types (Status(..))
4344
4445import Language.Javascript.JSaddle.Types (JSM(..), JSContextRef(..))
4546import qualified Network.Wai as W
46− (responseLBS, requestMethod, pathInfo)
47+ (responseLBS, requestMethod, pathInfo, modifyResponse, responseStatus)
4748import qualified Data.Text as T (pack)
4849import qualified Network.HTTP.Types as H
4950 (status403, status200)
@@ -111,7 +112,11 @@ jsaddleOr opts entryPoint otherApp = do
111112 Just handler -> do
112113 next <- encode <$> handler result
113114 sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/json")] next
114− _ -> otherApp req sendResponse
115+ (method, _) -> (catch404 otherApp) req sendResponse
116+ where catch404 = W.modifyResponse $ \resp ->
117+ case (method, W.responseStatus resp) of
118+ ("GET", Status 404 _) -> indexResponse
119+ _ -> resp
115120 return $ websocketsOr opts wsApp syncHandler
116121
117122
@@ -132,9 +137,12 @@ jsaddleWithAppOr opts entryPoint otherApp = jsaddleOr opts entryPoint $ \req sen
132137jsaddleAppPartial :: Request -> (Response -> IO ResponseReceived) -> Maybe (IO ResponseReceived)
133138jsaddleAppPartial = jsaddleAppPartialWithJs $ jsaddleJs False
134139
140+indexResponse :: Response
141+indexResponse = W.responseLBS H.status200 [("Content-Type", "text/html")] indexHtml
142+
135143jsaddleAppPartialWithJs :: ByteString -> Request -> (Response -> IO ResponseReceived) -> Maybe (IO ResponseReceived)
136144jsaddleAppPartialWithJs js req sendResponse = case (W.requestMethod req, W.pathInfo req) of
137− ("GET", []) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "text/html")] indexHtml
145+ ("GET", []) -> Just $ sendResponse indexResponse
138146 ("GET", ["jsaddle.js"]) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/javascript")] js
139147 _ -> Nothing
140148