Skip to content

Commit

Permalink
Remove livejs script tag on render
Browse files Browse the repository at this point in the history
  • Loading branch information
jmglov committed Nov 7, 2023
1 parent 58d5557 commit 6f3fcda
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/quickblog/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@
:as opts}
(-> opts
apply-default-opts
(assoc :watch "<script type=\"text/javascript\" src=\"https://livejs.com/live.js\"></script>")
(assoc :watch (format "<script type=\"text/javascript\" src=\"%s\"></script>"
lib/live-reload-script))
render)]
(reset! posts-cache (:posts opts))
(serve opts false)
Expand Down
8 changes: 7 additions & 1 deletion src/quickblog/internal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
[markdown.core :as md]
[selmer.parser :as selmer]))

;; Script used for live reloading in watch mode
(def live-reload-script "https://livejs.com/live.js")

(def ^:private cache-filename "cache.edn")
(def ^:private resource-path "quickblog")
(def ^:private templates-resource-dir "templates")
Expand Down Expand Up @@ -273,7 +276,10 @@
post-file (fs/file posts-dir file)]
(or force-render
(rendering-modified? out-file
(cons post-file rendering-system-files))))))
(cons post-file rendering-system-files))
;; watch mode adds a live reloading script, which should be
;; removed on subsequent renders
(str/includes? (slurp out-file) live-reload-script)))))
(map first)
set))

Expand Down
23 changes: 21 additions & 2 deletions test/quickblog/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[babashka.fs :as fs]
[quickblog.api :as api])
[quickblog.api :as api]
[quickblog.internal :as lib])
(:import (java.util UUID)))

(def test-dir ".test")
Expand Down Expand Up @@ -197,7 +198,25 @@
:templates-dir templates-dir
:cache-dir cache-dir
:out-dir out-dir})
(is (str/includes? (slurp (fs/file out-dir "comments.html")) "<!-- a comment -->")))))
(is (str/includes? (slurp (fs/file out-dir "comments.html")) "<!-- a comment -->"))))

(testing "remove live reloading on render"
(with-dirs [posts-dir
templates-dir
cache-dir
out-dir]
(write-test-post posts-dir)
(api/render {:posts-dir posts-dir
:templates-dir templates-dir
:cache-dir cache-dir
:out-dir out-dir
:watch lib/live-reload-script})
(is (str/includes? (slurp (fs/file out-dir "test.html")) lib/live-reload-script))
(api/render {:posts-dir posts-dir
:templates-dir templates-dir
:cache-dir cache-dir
:out-dir out-dir})
(is (not (str/includes? (slurp (fs/file out-dir "test.html")) lib/live-reload-script))))))

;; disabled, flaky in CI, cc @jmglov
#_(deftest caching
Expand Down

0 comments on commit 6f3fcda

Please sign in to comment.