Merge pull request #733 from evaogbe/static-mime-type

Add mime-types option to static handler
This commit is contained in:
Joel Kaasinen 2025-04-08 08:24:22 +03:00 committed by GitHub
commit 78cf477b88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 36 additions and 7 deletions

1
.gitignore vendored
View file

@ -15,3 +15,4 @@ figwheel_server.log
/.idea
.clj-kondo
.shadow-cljs
.cache

View file

@ -12,6 +12,12 @@ We use [Break Versioning][breakver]. The version numbers follow a `<major>.<mino
[breakver]: https://github.com/ptaoussanis/encore/blob/master/BREAK-VERSIONING.md
## UNRELEASED
* Improvements to mime type handling in `create-file-handler` and `create-resource-handler` [#733](https://github.com/metosin/reitit/pull/733)
* New `:mime-types` option to configure a map from file extension to mime type
* Don't set Content-Type header at all if mime type is not known
## 0.8.0 (2025-03-28)
**[compare](https://github.com/metosin/reitit/compare/0.7.2..0.8.0)**

View file

@ -0,0 +1,3 @@
{
"name": "Example"
}

View file

@ -227,7 +227,7 @@
;; TODO: ring.middleware.head/wrap-head
;; TODO: handle etags
(defn -create-file-or-resource-handler
[response-fn {:keys [parameter root path loader allow-symlinks? index-files index-redirect? canonicalize-uris? paths not-found-handler]
[response-fn {:keys [parameter root path loader allow-symlinks? index-files index-redirect? canonicalize-uris? paths not-found-handler mime-types]
:or {parameter (keyword "")
root "public"
index-files ["index.html"]
@ -250,9 +250,11 @@
join-paths (fn [& paths]
(str/replace (str/replace (str/join "/" paths) #"([/]+)" "/") #"/$" ""))
response (fn [path]
(if-let [response (or (paths (join-paths "/" path))
(when-let [response (or (paths (join-paths "/" path))
(response-fn path options))]
(response/content-type response (mime-type/ext-mime-type path))))
(if-let [content-type (mime-type/ext-mime-type path mime-types)]
(response/content-type response content-type)
response)))
path-or-index-response (fn [path uri]
(or (response path)
(when (or canonicalize-uris? (str/ends-with? uri "/"))
@ -295,7 +297,8 @@
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
| :index-redirect? | optional boolean: if true (default false), redirect to index file, if false serve it directly
| :canonicalize-uris? | optional boolean: if true (default), try to serve index files for non directory paths (paths that end with slash)
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)"
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)
| :mime-types | optional map of filename extensions to mime-types that will be used to guess the content type in addition to the ones defined in ring.util.mime-type/default-mime-types"
([]
(create-resource-handler nil))
([opts]
@ -314,7 +317,8 @@
| :index-files | optional vector of index-files to look in a resource directory, defaults to `[\"index.html\"]`
| :index-redirect? | optional boolean: if true (default false), redirect to index file, if false serve it directly
| :canonicalize-uris? | optional boolean: if true (default), try to serve index files for non directory paths (paths that end with slash)
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)"
| :not-found-handler | optional handler function to use if the requested resource is missing (404 Not Found)
| :mime-types | optional map of filename extensions to mime-types that will be used to guess the content type in addition to the ones defined in ring.util.mime-type/default-mime-types"
([]
(create-file-handler nil))
([opts]

View file

@ -742,7 +742,22 @@
(let [response (app (request "/docs/"))]
(is (= (redirect "/docs/index.html") response)))
(let [response (app (request "/foobar"))]
(is (= 404 (:status response))))))))))))
(is (= 404 (:status response)))))))
(testing "with additional mime types"
(let [app (ring/ring-handler
(ring/router
["/*" (create {:mime-types {"webmanifest" "application/manifest+json"}})])
(ring/create-default-handler))
response (app (request "/site.webmanifest"))]
(is (= "application/manifest+json" (get-in response [:headers "Content-Type"])))))
(testing "when content type cannot be guessed"
(let [app (ring/ring-handler
(ring/router
["/*" (create nil)])
(ring/create-default-handler))
response (app (request "/site.webmanifest"))]
(is (not (contains? (:headers response) "Content-Type"))))))))))
#?(:clj
(deftest file-resource-handler-not-found-test