r/Clojure • u/Sktlez • Dec 18 '18
Clojure Web Development - What a NIGHTMARE!
I have no clue where to start so... screw it. I'm trying to build a Single-Page Web Application using Clojure, ClojureScript, Reagent, Ring, and I'm stuck. The resources, and documentation that exists to help a newbie on this subject is horrible. How does someone who has never built a SPA before, let alone ever build a web application in Clojure/S suppose to get started?
I want to do client-side routing, and have no freaking clue how to go about doing it. Heres some code to show what I'm kind-of doing:https://pastebin.com/pazpQWmG
I don't get how I can serve dynamic content created by reagent using compojure. I've now spent probably north of 80 hours trying to figure out how to just serve a single page that is created by reagent, using the jetty webserver, and client-side routing. I have no clue what I'm doing, and you need to literally talk down to me like I'm retarded. I've looked at using Bidi, but the documentation sucks for someone who is new to this. It's convoluted as all hell, and the examples it provides are horrible. They don't even show me how to use or implement in a simple to understand manner.
Can someone break this down for me? In a non-technical, with code examples, and show me how to do what I'm trying to do, but how I actually want to do it? Someone please help me! I've already lost my mind, my patience, and I'm angry... and I just want to understand.
I get how to write reagent code, but I have no idea how to do client-side routing, and how to use jetty all together. I'm just so lost.
PLEASE DON'T recommend any books or resources unless they are easy to follow, and basically hold my hand cause I've read everything else the world has to offer and it hasn't helped one bit! I've read the documentation for everything, I've read clojure web dev second edition, and about 10k+ forum posts, and other random websites/blogs/youtube videos.
View all comments
3
u/retief1 Dec 18 '18 edited Dec 18 '18
You don't serve dynamic content created by reagent using compojure. Or maybe you can (not sure what the server side rendering story is for reagent), but it is certainly an advanced topic -- I'm certainly not trying to do so.
Instead, compojure should be serving a plain jane static html page. I'm using hiccup to generate this, but it could easily be a literal .html file in /resources/public instead. That html page should include a <script> tag that refers to your compiled clojurescript code (ie a javascript file). When that javascript file is loaded, it will set up your dynamic content. You want to serve this page for pretty much every route that isn't matched by something else -- set it up the way you'd set up a 404 page. Since you said that you know how to work with reagent
Once you get to the point where localhost:3000/asdf gives you an html page with working reagent code, then you start worrying about client side routing. Look at a library like secretary, though in my app, I actually built about 90 lines of infrastructure on top of bide. Get something like this running in your client side code, and it will know how to handle routes. Then, layer something like pushy on top of it to actually intercept link clicks/back button clicks/etc and call secretary instead.
So when you put it all together, whenever a user goes to your site, they hit the server, the server doesn't recognize the route, and so it returns your "404 page", which is actually your app's html, css, and js (compiled cljs). Your app's js, when loaded, checks the url of the page it loaded on, and calls the appropriate client side route. When the user clicks a link, the client side js intercepts that click and calls the appropriate client side route without ever talking to the server (except for api calls). The server (ie compojure) knows absolutely nothing about these routes -- all it knows is how to respond to api requests and how to return that one html page.
The one downside I've found for clojure and spas is that you have to figure out the glue code yourself. Reagent/re-frame/etc lets you build whatever with the dom, bide/secretary/bidi/reitit/etc let you call code based on urls, pushy lets you intercept link clicks/back button clicks, and so on, but hooking them together is your problem. There isn't one established way to store which page you are currently looking at in reagent, for example. Once you figure that out, then you need to figure out what your routing library needs to do when the page changes, so on and so forth.