railsware / js-routes

Brings Rails named routes to javascript
http://railsware.github.io/js-routes/
MIT License
1.61k stars 151 forks source link

Missing ActiveAdmin routes on production #317

Closed dplaza closed 1 month ago

dplaza commented 1 month ago

I just upgraded to the latest version (2.2.10) and I followed the steps to implement the new middleware, and the rake task during deployment, as well as adding routes.js and routes.d.ts to .gitignore.

However, I'm encountering an issue: the routes generated in development and production environments are inconsistent, specifically the ActiveAdmin routes. Here's a simplified example:

RAILS_ENV=development rails js:routes
cat app/javascript/routes.js | grep admin_companies

# Result
export const admin_companies_url = ...
RAILS_ENV=production rails js:routes
cat app/javascript/routes.js | grep admin_companies

# No results

Interestingly, this only affects certain ActiveAdmin routes. The routes used by React components (which use JS routes) on AA are fine, but the ones on classic ActiveAdmin views are missing.

By the way, running rails routes in both development and production environments returns consistent results.

I suspect the production rake task is filtering out routes that aren't used by JS. Is there a way to prevent this?

Additional context

# routes.rb
Rails.application.routes.draw do
  ...
  ActiveAdmin.routes(self)
  ...
end

# Rakefile
...
task "assets:precompile" => "js:routes"

# development.rb
Rails.application.configure do
  ...
  config.middleware.use(JsRoutes::Middleware)
end

Thanks!

bogdan commented 1 month ago

I can not reproduce that with a new repo: https://github.com/bogdan/js-routes-test

There need to be some specifics to your problem. Can you reproduce it in public repo?

Also you may do this:

task "assets:precompile" => "routes:inspect"

namescpase :routes do
  task inspect: :environment do
    puts Rails.application.routes.named_routes.map do |route|
  route.name if route.name&.match?(/admin_companies/)
end.compact.inspect
  end
end

And see if routes are defined correctly. If they are, it is hard to imagine why JsRoutes doesn't catch them. Becuase it expects them to be in the same place:

https://github.com/railsware/js-routes/blob/master/lib/js_routes/instance.rb#L124

Maybe you have some bug with exclude/include options for your production env.

dplaza commented 1 month ago

Thanks for the quick response, @bogdan! I tried your suggestion, and indeed, the missing routes were not there either.

Good news tho! after some investigation, I was able to pinpoint the issue. The company model in our project has an encrypted field that uses the ignore_case: true option. This option triggers a database check to verify the existence of a specific column. Since we use this model in ActiveAdmin, when Rails tried to build the routes for it, an error was being caught and silently handled, allowing the rest of the routes to load but excluding all the admin_companies URLs.

We solved the issue by using nulldb during precompilation, and now everything is working smoothly! 🎉

Thanks again for looking at this so quickly, really appreciate it!