zio-http
zio-http copied to clipboard
Smart content-type response headers for static files
When serving static files, it would be nice if the content-type header was automatically filled for common file types (ex: .js files get application/javascript, etc).
http4s does this here: https://github.com/http4s/http4s/blob/main/core/src/main/scala/org/http4s/StaticFile.scala#L240 https://github.com/http4s/http4s/blob/main/core/src/main/scala/org/http4s/MimeDB.scala
When the content-type is not filled, browsers might refuse to load javascript files.
The links are broken. Stable links:
- MimeDB.scala
- Generated by MimeLoader.scala from jshttp/mime-db
- StaticFile.scala
We currently serve static files by converting the file content into ZStream of content. Great if we have a middleware that can attach content-type header smartly as @ghostdogpr mentioned
API suggestion
app @@ smartContentType(url, headerValue)
Spec
- Add middleware smartContentType
- Modfiy FileStreaming example to showcase usage of smartContentType header
I'm interested in taking a stab at this. How should this be implemented as a middleware? The middleware would need access to the file to parse the mimetype, which is not supported as far as I can tell.
Why don't we add a method on HttpApp instead that parses the mimetype and returns the file content as a ZStream?
I just realized we don't need to get the mime type from the file if we base ourselves on the file extension in the url.
Does this resemble what you had in mind? https://github.com/olivierdeckers/zio-http/commit/8abfa9274fd222c59e7c2bdde6bff18ed31c723a
I think this is done (Http.fromFile etc are using MimeDb to set the content type)