DEV Community

Alexander Oloo
Alexander Oloo

Posted on • Originally published at alexanderoloo.com on

Gotcha! It’s the trailing slash

You’ve discovered the Luminus framework. You’ve reached unparalleled levels of productivity. You can build a web app at the drop of a hat. You can’t believe no-one told you about this before. You’re life is perfect except for one small thing.

Every now and then an existing route returns a 404. It doesn’t make sense. The error only occurs some of the time. Should that even be possible? You want to pull you hair out…

GOTCHA! It’s the trailing slash!

Luminus relies on the awesome routing library by reitit. And reitit uses exact route matching. So /apply and /apply/ are two different routes. Therefore depending on how your end user enters the url, they may or may not get a 404.

Here’s how I fixed it:

(mount/defstate app
  ...
      (ring/routes
        (swagger-ui/create-swagger-ui-handler
          {:path "/swagger-ui"
           :url "/api/swagger.json"
           :config {:validator-url nil}})
        (ring/create-resource-handler
          {:path "/"})
        (wrap-content-type
          (wrap-webjars (constantly nil)))
        ; Here is it is => 
        (ring/redirect-trailing-slash-handler {:method :strip})
        ;<= and you're good to go
        (ring/create-default-handler
  ...

You can learn more about handling the trailing slash here.

May your build always pass.

Alex

This post is part of the "Advent of Parens".

Today’s post is late courtesy of our wonderful electricity provider Eskom. Loadshedding is the best!

Top comments (0)