How to use catch all routes with Kemal
For a Vue.js single page application – at least on a SUB URL of our page – we want a catch all on our server, which will always render the same template / HTML output to the browser.
Kemal is internally based on Radix for routing:
Radix has a Catch All / Glob character: *
Therefore, if we want to create a catch all route, we simply write:
get “/debug/*” do |env|
render “src/views/hello.ecr”
end
This will match, for example:
- /debug
- /debug/
- /debug/anything
- /debug/anything/as/deep/as/you/want
This will NOT match:
- /debug3
Further reading / Code:
- https://github.com/luislavena/radix/blob/master/src/radix/tree.cr – look for private def find(path : String, result : Result, node : Node, first = false) – in line 256 at the time of writing, the glob * is defined