From fddedf4143e46090f9a6501ac495a7cbd35c3a14 Mon Sep 17 00:00:00 2001 From: Juho Teperi Date: Mon, 16 Apr 2018 20:41:18 +0300 Subject: [PATCH] Use md5 hash for demo site cache busting instead of build timestamp --- build-example-site.sh | 2 +- demo/sitetools/prerender.cljs | 14 ++++---- demo/sitetools/server.cljs | 68 +++++++++++++++++++---------------- package-lock.json | 6 ++++ package.json | 3 +- 5 files changed, 53 insertions(+), 40 deletions(-) diff --git a/build-example-site.sh b/build-example-site.sh index 1bfc3b3..50735d9 100755 --- a/build-example-site.sh +++ b/build-example-site.sh @@ -6,7 +6,7 @@ SHA=$(git rev-parse HEAD) lein 'do' clean, cljsbuild once prod-npm, cljsbuild once prerender -node target/cljsbuild/prerender/main.js +node target/cljsbuild/prerender/main.js target/prod-npm/public/ lein codox diff --git a/demo/sitetools/prerender.cljs b/demo/sitetools/prerender.cljs index 20cdc32..f3508a4 100644 --- a/demo/sitetools/prerender.cljs +++ b/demo/sitetools/prerender.cljs @@ -2,18 +2,18 @@ (:require [reagent.debug :refer-macros [log]] [sitetools.core :as tools] [sitetools.server :as server] - [reagentdemo.core :as demo])) + [reagentdemo.core :as demo] + [path :as path])) (defn -main [& args] (log "Generating site") (demo/init!) - (let [conf @tools/config - conf (assoc conf :timestamp (str "?" (js/Date.now))) - {:keys [site-dir pages]} conf] + (let [[js-resource-path] args + {:keys [site-dir pages] :as conf} (assoc @tools/config :js-resource-path js-resource-path)] + (server/write-resources site-dir conf) (doseq [f (keys pages)] - (server/write-file (->> f tools/to-relative (server/path-join site-dir)) - (server/gen-page f conf))) - (server/write-resources site-dir conf)) + (server/write-file (->> f tools/to-relative (path/join site-dir)) + (server/gen-page f conf)))) (log "Wrote site") (js/process.exit 0)) diff --git a/demo/sitetools/server.cljs b/demo/sitetools/server.cljs index 221ff86..9943ea1 100644 --- a/demo/sitetools/server.cljs +++ b/demo/sitetools/server.cljs @@ -6,7 +6,11 @@ [reagent.dom.server :as server] [reagent.debug :refer-macros [dbg log dev?]] [reagent.interop :as i :refer-macros [$ $!]] - [sitetools.core :as tools])) + [sitetools.core :as tools] + [md5-file :as md5-file] + [path :as path] + [fs :as fs] + [path :as path])) ;;; Static site generation @@ -18,23 +22,31 @@ (defn danger [t s] [t {:dangerouslySetInnerHTML {:__html s}}]) -(defn html-template [{:keys [title body-html timestamp page-conf - js-file css-file main-div]}] - (let [main (str js-file timestamp)] - (server/render-to-static-markup - [:html - [:head - [:meta {:charset 'utf-8}] - [:meta {:name 'viewport - :content "width=device-width, initial-scale=1.0"}] - [:base {:href (-> page-conf :page-path base)}] - [:link {:href (str css-file timestamp) :rel 'stylesheet}] - [:title title]] - [:body - [:div {:id main-div} (danger :div body-html)] - (danger :script (str "var pageConfig = " - (-> page-conf clj->js js/JSON.stringify))) - [:script {:src main :type "text/javascript"}]]]))) +(defn add-cache-buster [resource-path path] + (let [h (md5-file/sync (path/join resource-path path))] + (str path "?" (subs h 0 6)))) + +(defn html-template [{:keys [title body-html page-conf + js-file css-file main-div + js-resource-path site-dir]}] + (server/render-to-static-markup + [:html + [:head + [:meta {:charset 'utf-8}] + [:meta {:name 'viewport + :content "width=device-width, initial-scale=1.0"}] + [:base {:href (-> page-conf :page-path base)}] + [:link {:href (add-cache-buster site-dir css-file) + :rel "stylesheet"}] + [:title title]] + [:body + [:div + {:id main-div} + (danger :div body-html)] + (danger :script (str "var pageConfig = " + (-> page-conf clj->js js/JSON.stringify))) + [:script {:src (add-cache-buster js-resource-path js-file) + :type "text/javascript"}]]])) (defn gen-page [page-path conf] (tools/emit [:set-page page-path]) @@ -46,26 +58,20 @@ :page-conf {:page-path page-path} :body-html bhtml))))) -(defn fs [] (js/require "fs")) -(defn path [] (js/require "path")) - (defn mkdirs [f] (doseq [d (reductions #(str %1 "/" %2) - (-> ($ (path) normalize f) + (-> (path/normalize f) (string/split #"/")))] - (when-not ($ (fs) existsSync d) - ($ (fs) mkdirSync d)))) + (when-not (fs/existsSync d) + (fs/mkdirSync d)))) (defn write-file [f content] (log "Write" f) - (mkdirs ($ (path) dirname f)) - ($ (fs) writeFileSync f content)) - -(defn path-join [& paths] - (apply ($ (path) :join) paths)) + (mkdirs (path/dirname f)) + (fs/writeFileSync f content)) (defn write-resources [dir {:keys [css-file css-infiles]}] - (write-file (path-join dir css-file) + (write-file (path/join dir css-file) (->> css-infiles - (map #($ (fs) readFileSync %)) + (map #(fs/readFileSync %)) (string/join "\n")))) diff --git a/package-lock.json b/package-lock.json index 0c3141b..a697e01 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3922,6 +3922,12 @@ "integrity": "sha1-2TPOuSBdgr3PSIb2dCvcK03qFG0=", "dev": true }, + "md5-file": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/md5-file/-/md5-file-4.0.0.tgz", + "integrity": "sha512-UC0qFwyAjn4YdPpKaDNw6gNxRf7Mcx7jC1UGCY4boCzgvU2Aoc1mOGzTtrjjLKhM5ivsnhoKpQVxKPp+1j1qwg==", + "dev": true + }, "md5.js": { "version": "1.3.4", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.4.tgz", diff --git a/package.json b/package.json index ab193d0..92669f7 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "karma": "2.0.0", "karma-chrome-launcher": "2.2.0", "karma-cljs-test": "0.1.0", - "karma-junit-reporter": "1.2.0" + "karma-junit-reporter": "1.2.0", + "md5-file": "^4.0.0" } }