Fork me on GitHub
#clojurescript
<
2020-03-29
>
Spaceman19:03:06

I am sending an index-handler on "/this-route":

(defn index-handler [req]
  (assoc (resource-response "index.html" {:root "public"})
         :headers {"Content-Type" "text/html; charset=UTF-8"}))

(def routes ["" {"/this-route" {:get index-handler}}]) ;; works
Which works fine.  But when I append anything to this-route, I'm unable to send index-handler even though I can still send a basic res/response:
(def routes ["" {"/this-route" {"" {:get index-handler} ;; doesn't work
                                "/something" {:get index-handler} ;; doesn't work
                                "/something-else" (res/response "some response") ;; works  

}}])
I get the errors in the client side console showing an error in the index.html line, where I start my app:
<script type="text/javascript">myapp.system.go();</script>
and the error is "myapp is not defined." Why might this be, and what am I doing wrong?

Spaceman19:03:21

index.html <html lang="en"> <head> <title>My App</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link href="css/style.css" rel="stylesheet" type="text/css"> <link rel="shortcut icon" type="image/png" href="favicon1.png"/> </head> <body> <div id="app"></div> <script src="js/compiled/myapp.js" type="text/javascript"></script> <script type="text/javascript">myapp.system.go();</script> </body> </html>

Spaceman19:03:30

Also:

DevTools failed to load SourceMap: Could not load content for : HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME

p-himik19:03:13

That's completely irrelevant, it's just that AdBlock doesn't provide source maps.

Quentin Le Guennec19:03:04

@pshar10 do you have a myapp.system namespace with a go function defined and exported?

Quentin Le Guennec19:03:35

also does js/compiled/myapp.js exists?

Spaceman20:03:05

This is my myapp.system

(ns myapp.system
  (:require [com.stuartsierra.component :as component]
            [myapp.components.ui :refer [new-ui-component]]))

(declare system)

(defn new-system []
  (component/system-map
   :app-root (new-ui-component)))

(defn init []
  (set! system (new-system)))

(defn start []
  (set! system (component/start system)))

(defn stop []
  (set! system (component/stop system)))

(defn ^:export go []
  (init)
  (start))

(defn reset []
  (stop)
  (go))

Spaceman20:03:57

This is js/compiled/myapp.js

var CLOSURE_UNCOMPILED_DEFINES = {};
var CLOSURE_NO_DEPS = true;
if(typeof goog == "undefined") document.write('<script src="js/compiled/out/goog/base.js"></script>');
document.write('<script src="js/compiled/out/goog/deps.js"></script>');
document.write('<script src="js/compiled/out/cljs_deps.js"></script>');
document.write('<script>if (typeof goog == "undefined") console.warn("ClojureScript could not load :main, did you forget to specify :asset-path?");</script>');
document.write('<script>goog.require("devtools.preload");</script>');
document.write('<script>goog.require("figwheel.connect");</script>');
document.write('<script>goog.require("process.env");</script>');
document.write('<script>goog.require("cljs.user");</script>');

document.write("<script>figwheel.connect.start();</script>");

dpsutton20:03:22

Note that says figwheel connect start

Spaceman20:03:32

Noted. Is it supposed to be something else?

Quentin Le Guennec20:03:08

What exactly is your issue?

dpsutton20:03:51

Recently you were trying to use shadow-cljs. Which build tool are you trying to use right now?

Spaceman20:03:54

The exact issue is: When I go to a route like localhost:5000/myroute, which returns index.html it works. When I go to a route like localhost:5000/myroute/another and return index.html it doesn't work. The error is: myapp is not defined at line <script type="text/javascript">myapp.system.go();</script> But when I go to a route like localhost:5000/myroute/another, and return a res/response, it works.

Petrus Theron20:03:37

@pshar10, I gave an answer to your question in #clojure. The "root" of the issue is absolute vs relative paths.

Spaceman20:03:31

I don't know. I did lein new chestnut myapp, and did (go), and then (cljs-repl). This issue wasn't there before.

Spaceman20:03:21

When I do (go), I see Figwheel: Stopped watching CSS Watcher paths ["resources/public/css"] Figwheel: Stopped watching build - app Figwheel: Stopping Websocket Server Figwheel: Starting server at http://127.0.0.1:3449 Figwheel: Watching build - app Compiling build :app to "dev-target/public/js/compiled/myapp.js" from ["src/cljs" "src/cljc" "dev"]... Successfully compiled build :app to "dev-target/public/js/compiled/myapp.js" in 0.93 seconds. Figwheel: Starting CSS Watcher for paths ["resources/public/css"] Started myapp on http://localhost:5000 :started

lilactown20:03:32

@pshar10 your files are relative paths

lilactown20:03:57

I would make sure that myapp.js is even loaded when accessing your HTML file at myroute/another

jaide21:03:22

A few months ago I think the cognitect team mentioned they don't usually use cider but some kind of inferior mode. Does anyone know what that setup is like? Are they configuring inferior-lisp or are they using inf-clojure?

Alex Miller (Clojure team)21:03:49

I think Rich and Stu both use inferior-lisp but I’m not positive. I use Cursive mainly.

jaide21:03:42

Ah that was it thanks! I'm trying to get spacemacs to play with lumo and wasn't sure if I could use inferior-lisp or inf-clojure.

andy.fingerhut22:03:26

Over the past couple of months I have used inf-clojure with success. I have only been using it for hitting one keystroke to cause the previous form to be sent to, and evaluated in, a socket REPL. I haven't tried using it for other features like looking up docs, etc.

👍 4
johnj22:03:35

some useful ones for saving you some typing C-c M-r inf-clojure-reload -- C-c C-b inf-clojure-eval-buffer -- C-c C-r inf-clojure-eval-region

andy.fingerhut22:03:16

I typically rebind the ones I care about to function keys on my keyboard, as I detest using the Control key so often.

johnj23:03:34

fair, that's one of the strength of emacs I gues, just posted default ones.

jaide23:03:45

@U0CMVHBL2 Are you able to get inf-clojure expressions to print results in cljs? I've got a lumo repl running, I can eval forms, but I can't get the output from printing in a promise to display.

jaide23:03:57

(.then (.resolve js/Promise "hello") println)

andy.fingerhut23:03:17

I haven't tried that. When you say "the output from printing a promise", is that some kind of expression you are trying to evaluate in the REPL? Or is it some kind of automatic printing as triggered by the some code delivering a value to the promise?

jaide23:03:52

Just added a code sample in this thread

andy.fingerhut00:03:35

I do not use cljs very often. I have lumo installed on a macOS system. Are you starting lumo with a socket REPL (if that is even possible)? Or are you starting lumo from Emacs?

andy.fingerhut00:03:24

Hmm, now I see lumo -h shows that there is a --socket-repl option to lumo

andy.fingerhut00:03:36

When I run this command in a terminal window: lumo --socket-repl 50505

andy.fingerhut00:03:59

Then in my Emacs window I use M-x inf-clojure-connect with host localhost and TCP port 50505 to connect to it, then copy and paste that expression (.then (.resolve js/Promise "hello") println) into the Emacs buffer with the REPL prompt...

andy.fingerhut00:03:21

I see in the Emacs window the output:

cljs.user=> (.then (.resolve js/Promise "hello") println)
cljs.user=> #object[Promise [object Promise]]
cljs.user=> 

andy.fingerhut00:03:35

In the terminal where I started lumo --socket-repl 50505 I see this output:

jaide00:03:14

Sorry about that, got distracted with dinner. What if you eval that using C-c C-c?

jaide00:03:24

I did inf-clojure-connect, localhost, 55555, pasted that expression and nothing is printed

jaide00:03:57

Lumo 1.10.1
ClojureScript 1.10.520
Node.js v11.13.0
 Docs: (doc function-name-here)
       (find-doc "part-of-name-here")
 Source: (source function-name-here)
 Exit: Control+D or :cljs/quit or exit

cljs.user=> (.then (.resolve js/Promise "hello") println)
cljs.user=> #object[Promise [object Promise]]
cljs.user=> 

andy.fingerhut01:03:25

Did you check the REPL in the terminal where you started lumo to see if anything appeared there?

jaide02:03:21

:man-facepalming: Ack! There it is....

jaide02:03:14

Is there a way to get output to print to connected clients though?

andy.fingerhut04:03:09

I don't know. Just knowing that it is in the terminal instead of the socket REPL is a clue that someone else may be able to take the ball and run with, though.

jaide05:03:45

Found another path: Use the inf-clojure fn and the inf-clojure-generic-cmd var to launch a separate Clojure process in spacemacs. Now printed output shows up in that repl.

jaide22:03:15

With socket repls, can they not handle side-effects like printing the results of a promise?

lilactown22:03:53

I think there’s more work to be done to route printing to REPL out than just connecting to a socket REPL

jaide23:03:12

Ah I'll see if I can find it. It seems to be evaluating my calls but yeah not seeing any output. Was curious if it was due to my setup or is a trade-off of socket repls.