Frank Koehl
Frank Koehl

Reputation: 3176

Rails 7 ActionController::RoutingError stimulus controller with outdated digest

Edit: A simpler example than my original question, my CSS is experiencing the same caching issue. Using the dartsass-rails gem, with the bin/rails dartsass:watch in development. Default configuration. Changes to my SASS files throw an error because Rails goes looking for an outdated digest.

ActionController::RoutingError (No route matches [GET] "/assets/application-01d05ee418a658bda88e5c4b0f5b5b1e7610df787a559e2151838d567ecfe62d.css"):

(Original post follows)

I have a stimulus controller app/javascript/editor/section.js. I am using a default Rails 7 configuration, using import maps along with Stimulus. I am running Puma locally in the development environment, with no changes to default configuration.

Whenever I make changes to my stimulus controller, the web server does not pick up the changes, and continues looking for the outdated controller digest, throwing 404 errors.

Javascript console errors like this:

GET http://localhost:3000/assets/editor/section-ca5da14b07bf02053399e5391d999b5870acc0d8783bb5d7844ce5a22f2278da.js 
net::ERR_ABORTED 404 (Not Found)

Failed to register controller: editor (controllers/editor_controller) Error: 404 Not Found 
http://localhost:3000/assets/editor/section-ca5da14b07bf02053399e5391d999b5870acc0d8783bb5d7844ce5a22f2278da.js 
imported from http://localhost:3000/assets/controllers/editor_controller-0…31577c25719d88e9ca691b7736994a3335959f5513834f370414835d3.js

Puma console also complains:

ActionController::RoutingError (No route matches [GET] "/assets/editor/section-ca5da14b07bf02053399e5391d999b5870acc0d8783bb5d7844ce5a22f2278da.js"):

The reason for the error is known: the file has changed and been rebuilt, so the digest has changed. I just don't understand why the updated file isn't being picked up right away.

I have observed the behavior in Firefox and Chrome, with the Disable HTTP Cache (when toolbox is open) option enabled for both. Restarting the Puma web server will also correct the issue.

Upvotes: 1

Views: 410

Answers (1)

Jason Kotchoff
Jason Kotchoff

Reputation: 204

I had a similar issue using stimulus with importmaps but my problem was caused by my stimulus controller being located in a view component in the app/components directory which the importmap-rails gem cache sweeping doesn't watch implicitly and so I had to add this line inside config/application.rb:

config.importmap.cache_sweepers << config.root.join("app/components")

It appears your stimulus controller is located in app/javascript which should be watched by default.

Perhaps you could try putting a debugger breakpoint in your local cache sweeping code?

ie.

  • ensure you have the 'debug' gem in your Gemfile
  • bundle open importmap-rails
  • add a debugger statement in various spots (eg. lib/engine.rb) to figure out what's going on

Upvotes: 0

Related Questions