When using before_all (which I am currently using for auth checks), I noticed my DB calls were duplicating. I checked into this and the cause seems to be these middleware filters are firing twice.
Example:
require "kemal"
before_all do |env|
puts "RUNNING THIS FOR #{env.request.path}; #{env.object_id}"
end
get "/" do |env|
"hello, home"
end
get "/about" do |env|
"hello, about"
end
Kemal.run
Output:
RUNNING THIS FOR /; 131141490140544
RUNNING THIS FOR /; 131141490140544
2026-06-06T11:50:46.948687Z INFO - kemal: 200 GET / 3.32ms
RUNNING THIS FOR /about; 131141490139392
RUNNING THIS FOR /about; 131141490139392
2026-06-06T11:50:48.969343Z INFO - kemal: 200 GET /about 3.44ms
I've tried swapping the order of route declarations and before_all, but the same result happens.
What does seem to work is using this instead, but I'm unsure about why this would cause the difference:
before_all "/*" do |env|
puts "RUNNING THIS FOR #{env.request.path}; #{env.object_id}"
end
But the issue now is this doesn't trigger for "/"
Thanks!
When using before_all (which I am currently using for auth checks), I noticed my DB calls were duplicating. I checked into this and the cause seems to be these middleware filters are firing twice.
Example:
Output:
I've tried swapping the order of route declarations and before_all, but the same result happens.
What does seem to work is using this instead, but I'm unsure about why this would cause the difference:
But the issue now is this doesn't trigger for "/"
Thanks!