@@ -5,7 +5,8 @@ module Network.Wai.Application.Static
55 ( -- * WAI application
66 staticApp
77 -- ** Settings
8− , defaultStaticSettings
8+ , defaultWebAppSettings
9+ , defaultFileServerSettings
910 , StaticSettings
1011 , ssFolder
1112 , ssMkRedirect
@@ -25,6 +26,7 @@ module Network.Wai.Application.Static
2526 , mimeTypeByExt
2627 , defaultMimeTypeByExt
2728 -- ** Finding files
29+ , Piece (..)
2830 , Pieces
2931 , pathFromPieces
3032 , unfixPathName
@@ -223,7 +225,7 @@ data Piece = Piece
223225 { pieceRaw :: FilePath
224226 , piecePretty :: T.Text
225227 }
226− deriving (Eq, Ord)
228+ deriving (Eq, Ord, Show)
227229type Pieces = [Piece]
228230
229231relativeDirFromPieces :: Pieces -> T.Text
@@ -272,6 +274,14 @@ checkPieces fileLookup indices pieces req maxAge
272274 else return $ Redirect $ pieces ++ [Piece "" ""]
273275 where
274276 headers = W.requestHeaders req
277+
278+ -- HTTP caching has a cache control header that you can set an expire time for a resource.
279+ -- Max-Age is easiest because it is a simple number
280+ -- a cache-control asset will only be downloaded once (if the browser maintains its cache)
281+ -- and the server will never be contacted for the resource again
282+ -- A second caching mechanism is ETag and last-modified
283+ -- this form of caching is not as good as the static- the browser can avoid downloading the file, but it always need to send a request with the etag value or the last-modified value to the server to see if its copy is up to date
284+ -- We should set a cache control and one of ETag or last-modifed whenever possible
275285 handleCache file =
276286 case (lookup "if-none-match" headers, fileGetHash file) of
277287 (Just lastHash, Just getHash) -> do
@@ -356,8 +366,18 @@ defaultMkRedirect pieces newPath
356366 where
357367 relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)
358368
359−defaultStaticSettings :: StaticSettings
360−defaultStaticSettings = StaticSettings
369+defaultWebAppSettings :: StaticSettings
370+defaultWebAppSettings = StaticSettings
371+ { ssFolder = fileSystemLookup "static"
372+ , ssMkRedirect = defaultMkRedirect
373+ , ssGetMimeType = return . defaultMimeTypeByExt . T.unpack . fileName
374+ , ssMaxAge = MaxAgeForever
375+ , ssListing = Nothing
376+ , ssIndices = []
377+ }
378+
379+defaultFileServerSettings :: StaticSettings
380+defaultFileServerSettings = StaticSettings
361381 { ssFolder = fileSystemLookup "static"
362382 , ssMkRedirect = defaultMkRedirect
363383 , ssGetMimeType = return . defaultMimeTypeByExt . T.unpack . fileName