8000 Add mime-types option to static handler by evaogbe · Pull Request #733 · metosin/reitit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add mime-types option to static handler #733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ figwheel_server.log
/.idea
.clj-kondo
.shadow-cljs
.cache
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand Down
3 changes: 3 additions & 0 deletions dev-resources/public/site.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Example"
}
16 changes: 10 additions & 6 deletions modules/reitit-ring/src/reitit/ring.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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))
(response-fn path options))]
(response/content-type response (mime-type/ext-mime-type path))))
(when-let [response (or (paths (join-paths "/" path))
(response-fn path options))]
(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 "/"))
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down
17 changes: 16 additions & 1 deletion test/cljc/reitit/ring_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading
0