kit-clj / kit

Lightweight, modular framework for scalable web development in Clojure
https://kit-clj.github.io/
MIT License
483 stars 44 forks source link

Route paths don't work properly without final "/" - address bar is blank, web page not responsive #69

Closed ieugen closed 2 years ago

ieugen commented 2 years ago

Hi,

I can't access routes mounted to "/example-route" . It turns address bar blank and page does not work in Firefox. I must use "/example-route/" .

I believe this is because routes, by default only listen to "/" and not to "" as well. This might be a non issue for more seasoned devs, but to me it is.

How I got here: I have a new porject started with html and htmx modules. I got an error because both are trying to use the same route "/" so I decided to move htmx to "/htmx-clicked" . I changed the entry to:

:reitit.routes/ui {:base-path "/htmx-clicked",
                    :env #ig/ref :system/env}

Opened http://localhost:3000/htmx-clicked and the address bar went blank and web page is not responding. I can't even right click inside of it in FF.

ieugen commented 2 years ago

I managed to fix this by rendering the same route at both "" and "/" .

Should the examples be updated? Should we mount the htmx example by default to anther route ?!

(defn ui-routes [_opts]
  [["" {:get home}]
   ["/" {:get home}]
   ["/clicked" {:post clicked}]])
yogthos commented 2 years ago

I think the proper way would be to do

(defn ui-routes [_opts]
  [""
   ["/" {:get home}]
   ["/clicked" {:post clicked}]])
ieugen commented 2 years ago

Thanks, should I update the examples (modules)?

yogthos commented 2 years ago

Yeah, that'd be a good idea if you have time. :)

ieugen commented 2 years ago

Well, your solution does not seem to work. I get the same behavior as in normal case.

yogthos commented 2 years ago

Oh sorry, misread the original question. Here's what the official docs for Reitit suggest here https://github.com/metosin/reitit/blob/master/doc/ring/slash_handler.md

ieugen commented 2 years ago

The answer is in the docs you sent. To have good errors, they recommend composing it with default handler:

handler.clj

(ring/routes
      ;; Handle trailing slahs in routes
      ;; https://github.com/metosin/reitit/blob/master/doc/ring/slash_handler.md
      (ring/redirect-trailing-slash-handler)
      (ring/create-default-handler
       {:not-found
        (constantly {:status 404, :body "Page not found"})
        :method-not-allowed
        (constantly {:status 405, :body "Not allowed"})
        :not-acceptable
        (constantly {:status 406, :body "Not acceptable"})}))