Conflicting routes when using Roda as the backend and React (react-router) as the frontend

25 views
Skip to first unread message

ghandara

unread,
Jul 2, 2021, 10:32:34 AMJul 2
to Roda
For general context I'm working on an app that uses Roda as an API server, primarily as companion to an Electron desktop app.

Additionally, Roda is also serving a small frontend app built with React for account management/authentication that sends requests to Rodauth paths. The React app is mounted in Roda as such:

route do |r|
  r.public
  r.root do
     File.read("public/index.html")
  end
end

I defined a few routes in the frontend using React Router which work fine as long as you navigate to them from the root path.

The issue I have is that after I navigate to one of these routes (i.e. "/sign-up") and then I refresh the page that next request is handled by Roda and I get a 404 since that path isn't defined in the backend.

If I "duplicate" the routes in Roda to match those in React Router and point them to the same static file I can get the behavior to work as expected:

r.root do
  File.read("public/index.html")
end

r.get "sign-up" do
  File.read("public/index.html")
end

What I wonder if there is a more efficient/cleaner way of handling this situation?

I'm new to Roda, so any insight you can offer will be highly appreciated.
Thanks!

Jeremy Evans

unread,
Jul 2, 2021, 10:58:18 AMJul 2
to ruby...@googlegroups.com
If you want to return the contents of public/index.html for multiple pages:

r.get ["", "sign-up"] do
  File.read("public/index.html")
end

Alternatively, you want want to respond to any GET request with contents of public/index:

r.get do
  File.read("public/index.html")
end

Thanks,
Jeremy

ghandara

unread,
Jul 3, 2021, 9:08:40 AMJul 3
to Roda
Thanks for the clarification Jeremy!

Just to confirm if I understood correctly:
If I try to implement the 2nd strategy (using a "catch-all" r.get), I'd have to define any other routes for GET requests I want Roda to handle before the one that yields public/index , otherwise all of the GET requests will default to public/index right?

Jeremy Evans

unread,
Jul 3, 2021, 1:42:48 PMJul 3
to ruby...@googlegroups.com
On Sat, Jul 3, 2021 at 6:08 AM ghandara <fma...@gmail.com> wrote:
Thanks for the clarification Jeremy!

Just to confirm if I understood correctly:
If I try to implement the 2nd strategy (using a "catch-all" r.get), I'd have to define any other routes for GET requests I want Roda to handle before the one that yields public/index , otherwise all of the GET requests will default to public/index right?

That is correct.

Thanks,
Jeremy
Reply all
Reply to author
Forward
0 new messages