yesodweb / wai

Haskell Web Application Interface
MIT License
834 stars 263 forks source link

wai-app-static: serve HTML files without extension #999

Open rniii opened 3 months ago

rniii commented 3 months ago

Currently index.html files can be served by ssIndices, but it'd be useful if you could serve HTML files without an extension, e.g. posts/mypost.html -> posts/mypost

Alternatives

wai-extra's rewrite middleware could be used to add the .html extension on every request, but this would then break index.html files. Conditionally rewriting on whether the path ends with a / is also possible, but this would cause a lot of confusion and wouldn't match how frameworks usually serve static HTML

rniii commented 3 months ago

Not so sure how to implement this with the current code, but I got a custom lookup function to do this as a bit of a hack:

lookup pieces =
  case reverse pieces of
    name : pieces'
      | T.null $ T.dropWhile (/= '.') (fromPiece name) ->
          defaultLookup . reverse $
            unsafeToPiece (fromPiece name <> ".html") : pieces'
    _ -> defaultLookup pieces