@@ -17,6 +17,7 @@ import Monocle.Prelude
1717import Monocle.Search.Query (loadAliases )
1818import Monocle.Servant.HTTP (server )
1919import Monocle.Servant.HTTPMain (RootAPI )
20+ import Network.HTTP.Types (hContentType )
2021import Network.HTTP.Types.Status qualified as HTTP
2122import Network.Wai qualified as Wai
2223import Network.Wai.Handler.Warp qualified as Warp
@@ -28,6 +29,7 @@ import Prometheus.Metric.GHC (ghcMetrics)
2829import Servant
2930import Servant.Auth.Server (CookieSettings (.. ), cookieXsrfSetting , defaultCookieSettings , defaultJWTSettings )
3031import System.Directory qualified
32+ import System.FilePath (takeExtension )
3133
3234import Effectful qualified as E
3335import Effectful.Concurrent.MVar qualified as E
@@ -62,6 +64,22 @@ mkStaticMiddleware publicUrl title webAppPath = do
6264 exist <- System.Directory. doesPathExist fp
6365 pure $ if exist then Just fp else otherFP
6466
67+ -- Content-Type for static files
68+ staticFileHeaders path =
69+ [
70+ ( hContentType
71+ , case takeExtension path of
72+ " .css" -> " text/css"
73+ " .js" -> " application/javascript"
74+ " .ico" -> " image/x-icon"
75+ " .png" -> " image/png"
76+ " .svg" -> " image/svg+xml"
77+ " .woff" -> " font/woff"
78+ " .woff2" -> " font/woff2"
79+ _ -> " application/octet-stream"
80+ )
81+ ]
82+
6583 -- The middleware pass the request to the monocle app
6684 staticMiddleware :: LByteString -> FilePath -> Wai. Application -> Wai. Application
6785 staticMiddleware index rootDir app' req waiRespond = app' req responder
@@ -76,11 +94,11 @@ mkStaticMiddleware publicUrl title webAppPath = do
7694 if Data.List. null reqPath || " .." `Data.List.isInfixOf` reqPath
7795 then -- The path is empty or fishy
7896 pure Nothing
79- else -- Checks if the request match a file, such as favico or css
97+ else -- Checks if the request matches a file, such as favico or css
8098 (rootDir <> reqPath) `existOr` Nothing
8199 waiRespond $ case respPath of
82- -- The path exist, returns it
83- Just path -> Wai. responseFile HTTP. status200 [] path Nothing
100+ -- The path exist, returns it with correct Content-Type
101+ Just path -> Wai. responseFile HTTP. status200 (staticFileHeaders path) path Nothing
84102 -- Otherwise returns the index
85103 Nothing -> Wai. responseLBS HTTP. status200 [] index
86104
0 commit comments