Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Cache Main App/Embed JS fonts (#68120)

Timofey Kachalov28baf732010d
diff --git a/src/metabase/request/util.clj b/src/metabase/request/util.clj
index 0116dbf870c..9ba759d08a7 100644
--- a/src/metabase/request/util.clj
+++ b/src/metabase/request/util.clj
@@ -41,8 +41,10 @@
(or
;; match requests that are js/css and have a cache-busting hex string
(re-matches #"^/app/dist/.+\.[a-f0-9]+\.(js|css)$" uri)
- ;; any resource that is named as a cache-busting hex string (e.g. fonts, images)
- (re-matches #"^/app/dist/[a-f0-9]+.*$" uri))))
+ ;; any resource that is named as a cache-busting hex string (e.g. images)
+ (re-matches #"^/app/dist/[a-f0-9]+.*$" uri)
+ ;; font files are static and should be cached
+ (re-matches #"^/app/fonts/.+\.(woff2?|ttf|otf|eot)$" uri))))
(defn https?
"True if the original request made by the frontend client (i.e., browser) was made over HTTPS.
diff --git a/test/metabase/request/util_test.clj b/test/metabase/request/util_test.clj
index f8e403905e6..dc42e06f46d 100644
--- a/test/metabase/request/util_test.clj
+++ b/test/metabase/request/util_test.clj
@@ -7,6 +7,24 @@
[metabase.test :as mt]
[ring.mock.request :as ring.mock]))
+(deftest ^:parallel cacheable?-test
+ (testing "JS/CSS with cache-busting hash are cacheable"
+ (is (some? (req.util/cacheable? {:request-method :get :uri "/app/dist/main.abc123def.js"})))
+ (is (some? (req.util/cacheable? {:request-method :get :uri "/app/dist/styles.abc123def.css"}))))
+ (testing "Resources in /app/dist/ with hex hash prefix are cacheable"
+ (is (some? (req.util/cacheable? {:request-method :get :uri "/app/dist/abc123def456.png"}))))
+ (testing "Font files are cacheable"
+ (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/Lato/lato-v16-latin-regular.woff2"})))
+ (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/Lato/lato-v16-latin-regular.woff"})))
+ (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/CustomFont/custom.ttf"})))
+ (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/CustomFont/custom.otf"})))
+ (is (some? (req.util/cacheable? {:request-method :get :uri "/app/fonts/CustomFont/custom.eot"}))))
+ (testing "Non-GET requests are not cacheable"
+ (is (not (req.util/cacheable? {:request-method :post :uri "/app/fonts/Lato/lato.woff2"}))))
+ (testing "Other paths are not cacheable"
+ (is (not (req.util/cacheable? {:request-method :get :uri "/api/dashboard/1"})))
+ (is (not (req.util/cacheable? {:request-method :get :uri "/app/dist/main.js"})))))
+
(deftest ^:parallel https?-test
(doseq [[headers expected] {{"x-forwarded-proto" "https"} true
{"x-forwarded-proto" "http"} false