This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-01
Channels
- # announcements (5)
- # aws (2)
- # babashka (35)
- # beginners (68)
- # bristol-clojurians (1)
- # calva (14)
- # cider (9)
- # clj-commons (5)
- # clojars (3)
- # clojure (101)
- # clojure-australia (6)
- # clojure-europe (35)
- # clojure-italy (2)
- # clojure-nl (5)
- # clojure-spec (1)
- # clojure-uk (4)
- # clojurescript (70)
- # conjure (3)
- # cursive (7)
- # datomic (36)
- # emacs (11)
- # exercism (1)
- # fulcro (23)
- # helix (2)
- # jobs-discuss (15)
- # lsp (17)
- # malli (8)
- # meander (1)
- # nrepl (60)
- # off-topic (10)
- # pedestal (9)
- # react (8)
- # reagent (53)
- # reveal (10)
- # shadow-cljs (27)
- # spacemacs (4)
- # tools-build (4)
- # tools-deps (4)
- # xtdb (2)
How to do I make a catch-all/not-found route? I.e. one that doesn’t cancel out the other routes. When I was using Compojure it was as simple as "*"
while making sure that the route was the last to be defined. Then the SPA gets served in the same HTML/JS blob and the frontend routing in the single-page app would basically take over.
With Pedestal it seems like you need separation into separate paths, e.g. /app
and /api
for this work…? And if you want both /app
and /app/*
to work you will need to make two separate routes. Is that the case?
I basically need all of this to get it to work and it’s still less elegant than Compojure since it forces me to use “/app” as a the root rather than “/”:
["/" :get redirect :route-name ::root]
["/app" :get single-page-app :route-name ::app]
["/app/*" :get single-page-app :route-name ::app-path]
There isn't a way with pedestal's routing - but what you can do is use interceptors for this
;; ----------------------------------------------------------------------------
(def serve-spa-on-404
"Intercepts any unhandled get requests and serves the SPA."
{:leave
(fn [context]
(if (and (not (http/response? (:response context)))
(= :get (:request-method (:request context))))
(assoc
context
:response
{:status 200
:headers {"Content-Type" "text/html"}
:body
(page/html5
{:lang "en"}
[:html
[:head
[:meta {:charset "UTF-8"}]
[:style "body { padding: 0; margin: 0; }"]]
[:body
[:div {:id "mount"}]
[:script {:type "text/javascript"} (-> (io/resource "public/app.js") ;; TODO: Cache
(slurp))]
[:script {:type "text/javascript"}
"var app = Elm.Main.init({node: document.getElementById('mount')});"]]])})
context))})