@@ -5,7 +5,8 @@ module Network.Wai.Application.Static
5 ( -- * WAI application
5 ( -- * WAI application
7 -- ** Settings
7 -- ** Settings
8− , defaultStaticSettings
8+ , defaultWebAppSettings
9+ , defaultFileServerSettings
9 , StaticSettings
10 , StaticSettings
10 , ssFolder
11 , ssFolder
11 , ssMkRedirect
12 , ssMkRedirect
@@ -25,6 +26,7 @@ module Network.Wai.Application.Static
25 , mimeTypeByExt
26 , mimeTypeByExt
26 , defaultMimeTypeByExt
27 , defaultMimeTypeByExt
27 -- ** Finding files
28 -- ** Finding files
29 , pathFromPieces
31 , pathFromPieces
30 , unfixPathName
32 , unfixPathName
@@ -223,7 +225,7 @@ data Piece = Piece
223 { pieceRaw :: FilePath
225 { pieceRaw :: FilePath
224 , piecePretty :: T.Text
226 , piecePretty :: T.Text
226− deriving (Eq, Ord)
228+ deriving (Eq, Ord, Show)
227type Pieces = [Piece]
229type Pieces = [Piece]
229relativeDirFromPieces :: Pieces -> T.Text
231relativeDirFromPieces :: Pieces -> T.Text
@@ -272,6 +274,14 @@ checkPieces fileLookup indices pieces req maxAge
272 else return $ Redirect $ pieces ++ [Piece "" ""]
274 else return $ Redirect $ pieces ++ [Piece "" ""]
274 headers = W.requestHeaders req
276 headers = W.requestHeaders req
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
275 handleCache file =
285 handleCache file =
276 case (lookup "if-none-match" headers, fileGetHash file) of
286 case (lookup "if-none-match" headers, fileGetHash file) of
277 (Just lastHash, Just getHash) -> do
287 (Just lastHash, Just getHash) -> do
@@ -356,8 +366,18 @@ defaultMkRedirect pieces newPath
357 relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)
367 relDir = TE.encodeUtf8 (relativeDirFromPieces pieces)
359−defaultStaticSettings :: StaticSettings
369+defaultWebAppSettings :: StaticSettings
360−defaultStaticSettings = StaticSettings
370+defaultWebAppSettings = StaticSettings
371+ { ssFolder = fileSystemLookup "static"
372+ , ssMkRedirect = defaultMkRedirect
373+ , ssGetMimeType = return . defaultMimeTypeByExt . T.unpack . fileName
374+ , ssMaxAge = MaxAgeForever
375+ , ssListing = Nothing
379+defaultFileServerSettings :: StaticSettings
380+defaultFileServerSettings = StaticSettings
361 { ssFolder = fileSystemLookup "static"
381 { ssFolder = fileSystemLookup "static"
362 , ssMkRedirect = defaultMkRedirect
382 , ssMkRedirect = defaultMkRedirect
363 , ssGetMimeType = return . defaultMimeTypeByExt . T.unpack . fileName
383 , ssGetMimeType = return . defaultMimeTypeByExt . T.unpack . fileName