reitit

sb 2024-09-18T19:10:25.098429Z

Is there any good idea how to handle trailing slashes in reitit.frontend routes? (rf/router routes {:data {:coercion..}} I saw this backlog ticket https://github.com/orgs/metosin/projects/4?pane=issue&itemId=23786291 .. just maybe somebody solved this issue already in easy way at reitit.frontend. Do I need to duplicate the sub-routes like [“sub-page1 {:name ,,,}] [“sub-page1/ {:name …}] ?

sb 2024-09-18T19:15:14.147739Z

that issue happens when I use {:use-fragment false}

2024-09-18T19:25:22.966449Z

I know you said for frontend, but I have an SPA and just use the redirect-trailing-slash-handler on the server so that there aren't trailing slashes https://github.com/metosin/reitit/blob/master/doc/ring/slash_handler.md not sure if that works in your case

👍 1
sb 2024-09-18T19:26:50.679769Z

I run from shadow-cljs,.. ok I’m thinking about.

sb 2024-09-19T14:22:11.528919Z

I drop here this is the solution, that works smoothly.. maybe useful for others too

(defn remove-trailing-slash [path]
  (if (clojure.string/ends-with? path "/")
    (subs path 0 (dec (count path)))
    path))

(def router
  (let [base-router (rf/router (get-routes))]
    (reify r/Router
      (match-by-path [_ path]
        (let [clean-path (if (> (count path) 1) (remove-trailing-slash path) path)
              {:as match :keys [path data]} (r/match-by-path base-router clean-path)
              submatch (when (and path (:router data))
                         (r/match-by-path (:router data)
                                          (clojure.string/replace path #"^/[^/]+" "")))]
          (cond-> match
            submatch (assoc-in [:data :submatch] submatch))))
      (match-by-name [_ name]
        (r/match-by-name base-router name {}))
      (match-by-name [_ name params]
        (or (->> (get-routes)
                 (some (fn [[prefix {r :router}]]
                         (when-let [m (and r (r/match-by-name r name params))]
                           (update m :path #(str (clojure.string/replace prefix #"/\*$" "") %))))))
            (r/match-by-name base-router name params)))
      (route-names [_]
        (r/route-names base-router)))))