Skip to content

Commit

Permalink
React dev workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Nov 11, 2024
1 parent dd97da2 commit 2bd2468
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
30 changes: 21 additions & 9 deletions src/nextjournal/clerk/builder.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]}]
Expand All @@ -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})
Expand Down
8 changes: 3 additions & 5 deletions src/nextjournal/clerk/render/hooks.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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"
Expand Down

0 comments on commit 2bd2468

Please sign in to comment.