haskellhaskell-wai

Customizing response headers in Wai middleware


I'm currently using wai-middleware-static to serve up custom pages for my server. However, i saw that my server was getting requests for favicon.ico, etc. on every page load, and also every single one of my web fonts, so i decided to check the cache settings on the response headers and found that there were none.

wai-middleware-static returns a Middleware value, which I think is a callback to a function provided by the middleware that is run on every request. Is there a way to modify this to add in a response header to tell the browser to cache the result?


Solution

  • Multiple middlewares can be chained together with normal function composition, e.g.:

    middleware1 . middleware2
    

    So if you had a middleware that added the cache settings to the response, you should be set. A basic structure that may help you is:

    addCacheSettings :: Middleware
    addCacheSettings innerApp request = do
        innerResponse <- innerApp request
        return $ myHelper innerResponse
      where
        myHelper :: Response -> Response
        myHelper = error "Your logic here"