diff --git a/src/nextjournal/clerk/builder.clj b/src/nextjournal/clerk/builder.clj index 1252f4d75..f9cd1d661 100644 --- a/src/nextjournal/clerk/builder.clj +++ b/src/nextjournal/clerk/builder.clj @@ -163,6 +163,25 @@ :path->doc path->doc :paths (vec (keys path->doc))))) +(defn- node-ssr! + [{:keys [viewer-js state] + :or {viewer-js + ;; for local REPL testing + "./public/js/viewer.js"}}] + (sh {:in (str "import '" viewer-js "';" + "globalThis.CLERK_SSR = true;" + "console.log(nextjournal.clerk.sci_env.ssr(" (pr-str (pr-str state)) "))")} + "node" + "--abort-on-uncaught-exception" + "--experimental-network-imports" + "--input-type=module" + "--trace-warnings")) + +(comment + (declare so) ;; captured in REPL in ssr! function + (node-ssr! {:state so}) + ) + (defn ssr! "Shells out to node to generate server-side-rendered html." [{:as static-app-opts :keys [report-fn resource->url]}] @@ -172,16 +191,9 @@ (dissoc :path->doc) (assoc :render-router :serve)) {duration :time-ms :keys [result]} - (eval/time-ms (sh {:in (str "import '" (resource->url "/js/viewer.js") "';" - "console.log(nextjournal.clerk.sci_env.ssr(" (pr-str (pr-str static-app-opts)) "))")} - "node" - "--abort-on-uncaught-exception" - "--experimental-network-imports" - "--input-type=module" - "--trace-warnings")) + (eval/time-ms (node-ssr! {:viewer-js (resource->url "/js/viewer.js") + :state static-app-opts})) {:keys [out err exit]} result] - #_(def s static-app-opts) - #_(prn static-app-opts) (if (= 0 exit) (do (report-fn {:stage :done :duration duration}) diff --git a/src/nextjournal/clerk/render/hooks.cljs b/src/nextjournal/clerk/render/hooks.cljs index 7982f21b3..83f6f203e 100644 --- a/src/nextjournal/clerk/render/hooks.cljs +++ b/src/nextjournal/clerk/render/hooks.cljs @@ -58,8 +58,6 @@ (WrappedState. (react/useState init))) (defn- specify-atom! [ref-obj] - #_(js/console.log "ref" ref-obj (js/Object.isFrozen ref-obj)) - #_(prn :ref ref-obj) (specify! ref-obj IDeref (-deref [^js this] (.-current this)) @@ -74,12 +72,12 @@ ([o f a b] (reset! o (f o a b))) ([o f a b xs] (reset! o (apply f o a b xs)))))) -#_(prn :specify (specify-atom! (doto #js {} (js/Object.freeze)))) - (defn use-ref "React hook: useRef. Can also be used like an atom." ([] (use-ref nil)) - ([init] (specify-atom! (react/useRef init)))) + ([init] (if js/CLERK_SSR + (atom init) + (specify-atom! (react/useRef init))))) (defn ^:private eval-fn "Invoke (f x) if f is a function, otherwise return f"