Ad

How Can I Pass A Path To A Statically Served React App From Rails Api

I'm having the following setup:

A create-react-app frontend with react-router that is bundled into a static asset.
This static asset is served from my rails-api project's public folder as static files. The react app uses the rails-api as backend.
When I go to / route, the react app is served and I can navigate my app.

How can I pass any subpaths like /admin to the react-router? I already have a catch-all route in config/routes.rb

get '*path', to: "application#fallback_index_html", constraints: lambda { |req|
    req.path.exclude? 'rails/active_storage'
  }

And I can get the relevant subpath in the controller:

  def fallback_index_html
    path = request.parameters['path']
    render file: 'public/index.html'
  end

But I don't know how to serve the react app and pass the path to it. Any ideas?

Ad

Answer

Turns out, the problem is that my ApplicationController is derived from ActionController::API instead of ActionController::Base. With the Base controller, it works like a charm. See also https://stackoverflow.com/a/43912670/526426

Ad
source: stackoverflow.com
Ad