railsware / js-routes

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

using the middleware with rails 8 breaks rails routes generation #319

Open francescob opened 4 days ago

francescob commented 4 days ago

I've upgraded my application to rails 8 and now the line config.middleware.use(JsRoutes::Middleware)

in development.rb causes the rails routes to be empty. Removing the line the application works as normal

le0pard commented 4 days ago

try

Rails.application.routes_reloader.try(:execute_unless_loaded)

before

config.middleware.use(JsRoutes::Middleware)
francescob commented 4 days ago

thanks for the super quick reply, unfortunately that line leads to the app failing to boot with the following error:

/home/francesco/.asdf/installs/ruby/3.3.4/lib64/ruby/gems/3.3.0/gems/railties-8.0.0/lib/rails/engine.rb:602:in unshift: can't modify frozen Array: [] (FrozenError)

le0pard commented 4 days ago

ok, looks like need to find best way to fix this (looks like similar to https://github.com/heartcombo/devise/issues/5705 )

le0pard commented 4 days ago

Maybe adding application.reload_routes_unless_loaded in beginning of https://github.com/railsware/js-routes/blob/9bedb7f20a0d125189f8bcd9883d7c21314b47e3/lib/js_routes/instance.rb#L25-L27 may fix problem. Are you able to test @francescob (create fork and modify it) ?

so it will be

     def generate
      # Ensure routes are loaded. If they're not, load them.
      application = T.unsafe(self.application)
      if named_routes.empty?
        if application.respond_to?(:reload_routes_unless_loaded, true)
          application.reload_routes_unless_loaded
        elsif application.respond_to?(:reload_routes!, true)
          application.reload_routes!
        end
      end
      ...
francescob commented 4 days ago

yes, that fixed it. I'm creating a pull request