diff --git a/src/quickblog/api.clj b/src/quickblog/api.clj
index 77596a1..9555d86 100644
--- a/src/quickblog/api.clj
+++ b/src/quickblog/api.clj
@@ -564,7 +564,8 @@
:as opts}
(-> opts
apply-default-opts
- (assoc :watch "")
+ (assoc :watch (format ""
+ lib/live-reload-script))
render)]
(reset! posts-cache (:posts opts))
(serve opts false)
diff --git a/src/quickblog/internal.clj b/src/quickblog/internal.clj
index 00ee8e5..d020ed7 100644
--- a/src/quickblog/internal.clj
+++ b/src/quickblog/internal.clj
@@ -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")
@@ -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))
diff --git a/test/quickblog/api_test.clj b/test/quickblog/api_test.clj
index 7734ecd..6b41914 100644
--- a/test/quickblog/api_test.clj
+++ b/test/quickblog/api_test.clj
@@ -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")
@@ -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")) "")))))
+ (is (str/includes? (slurp (fs/file out-dir "comments.html")) ""))))
+
+ (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