shadow-cljs

https://github.com/thheller/shadow-cljs | https://github.com/sponsors/thheller | https://www.patreon.com/thheller
Adrian Smith 2021-02-21T00:07:55.005300Z

I have a shadow-cljs project using a node-script build, I'm using shadow-cljs node-repl then connecting and trying evaluate my file that has npm imports like ["robotjs" as robo] when I try and evaluate the file I'm getting clojure.core.spec complaining about that, where am I going wrong?

Adrian Smith 2021-02-21T00:14:45.005600Z

ah looks like I had to do (shadow/node-repl) whilst inside the repl

thheller 2021-02-21T09:13:59.005900Z

indeed, REPLs first always start out in CLJ when you connect over nREPL

Adam Kalisz 2021-02-21T11:59:38.010400Z

@theller I don't know, if this came up earlier. For some time, I was unable to load the development version of our ClojureScript project on localhost in Chromium. It worked in Firefox, but was rather slow. I didn't know at all, what caused it. So I tried to gather some data about it. I cleared the cache of Chromium (deleted ~/.cache/chromium) and this helped a few times but the problem always returned. I have resorted to deleting of the build directory of shadow-cljs. The result was that the development version of our project loaded immediately in Chromium and Firefox as well. I don't have any further data, just wanted to let you know.

Remy 2021-02-21T13:17:49.012600Z

Hello, I’m trying to setup custom response headers in order to do a cross-origin request during development. I’ve been reading [the user guide](https://shadow-cljs.github.io/docs/UsersGuide.html) and [a stackoverflow question](https://stackoverflow.com/questions/65907176/how-to-properly-setup-shadow-cljs-for-hot-reload) but it seems the headers only apply to the shadow-cljs “dashboard” server (eg. 9630), not the app itself. I tried the following so far: - wrapping shadow.http.push-state/handle and calling the wrapper in :dev-http. Same result when overriding :push-state/headers - setting :devtools :http-handler, again pointing at my wrapper I just wanted to ask here before going through the source, maybe there’s something i misunderstood entirely from the docs. I’m starting the server up through emacs cljs-jack-in with

npx shadow-cljs -d nrepl/nrepl:0.8.3 -d cider/piggieback:0.5.2 -d cider/cider-nrepl:0.25.9 server
;; dev/custom_handler.clj
(ns custom-handler
  (:require [shadow.http.push-state :refer [handle]]))


(defn handle-wrapper
  [req]
  (handle (assoc-in req
                    [:http-config :push-state/headers]
                    {"conten-type" "text/html; charset=utf-8"
                     "access-control-allow-origin" "*"})))
;; shadow-cljs.edn
;; shadow-cljs 2.11.7
;; clojurescript 1.10.773
{:deps {:aliases [:dev :shadow :cljc]}
 :nrepl {:port 8777
         :middleware [refactor-nrepl.middleware/wrap-refactor]}

 :dev-http {8281 {
                  :root "resources/public"
                  :push-state/headers {"access-control-allow-origin" "*"}
                  }
            }
 :builds {:app {:target :browser
                :output-dir "resources/public/js/compiled"
                :asset-path "/js/compiled"
                :modules {:app {:init-fn remyrd.rock-n-call-frontend.core/init}}
                :devtools {:http-root "resources/public"
                           :http-port 8280
                           :http-handler custom-handler/handle-wrapper
                           :preloads [devtools.preload]
                           }}}}

thheller 2021-02-21T13:20:45.013600Z

basically as soon as you want to do something like this you should be using your own http server instead

thheller 2021-02-21T13:21:22.014200Z

what is serving your HTML that makes this necessary in the first place?

thheller 2021-02-21T13:22:05.014900Z

the handler isn't supposed to modify the request coming in. instead it should modify the response

thheller 2021-02-21T13:23:10.015100Z

(defn handle-wrapper
  [req]
  (-> req
      (handle)
      (update :headers merge
        {"access-control-allow-origin" "*"})))

thheller 2021-02-21T13:28:37.017300Z

also why do you have a dev-http and then another in devtools?

Remy 2021-02-21T13:30:33.018700Z

Right, silly me it should’ve gone in the response. I agree I should be using my own http server, for now i’m just doodling and I needed this data from an external api.

thheller 2021-02-21T13:30:52.018900Z

maybe you want to proxy instead?

Remy 2021-02-21T13:31:02.019Z

Just to showcase the two possibilities i tried

Remy 2021-02-21T13:33:04.019200Z

probably… i see there’s a section about it that i can read :)

Remy 2021-02-21T13:43:49.019400Z

Well i’m trying to figure out what is the best way to serve cross-origin content on a static webpage, probably I’m forcing this approach but I wanted to avoid spinning up a server

Remy 2021-02-21T13:45:00.020200Z

Btw, thanks for the quick answers and the tool

thheller 2021-02-21T13:50:38.020300Z

sounds like you are maybe trying to solve this in the wrong way? the API server handling the requests is the server that needs to set those CORS headers

thheller 2021-02-21T13:50:50.020500Z

not the shadow-cljs server serving the .js files

Remy 2021-02-21T13:54:27.020700Z

oh i see, yes the origin in allow origin makes more sense now, thanks