Use md5 hash for demo site cache busting instead of build timestamp

This commit is contained in:
Juho Teperi 2018-04-16 20:41:18 +03:00
parent 1ebe5e978b
commit fddedf4143
5 changed files with 53 additions and 40 deletions

View File

@ -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

View File

@ -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))

View File

@ -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"))))

6
package-lock.json generated
View File

@ -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",

View File

@ -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"
}
}