Struggle with Rails 7, esbuild, and Tailwind CSS Asset Loading
Recently, I ran into an issue while setting up esbuild and Tailwind CSS in my Rails 7 app. Despite following all the recommended steps, my stylesheets and JavaScript files simply wouldn't load in the browser. The Rails logs repeatedly showed:
ActionController::RoutingError (No route matches [GET] "/assets/application.css")
ActionController::RoutingError (No route matches [GET] "/assets/application.js")
What I Tried
- Correct Asset Tags in
application.html.erb
<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
<%= javascript_include_tag "application", "data-turbo-track": "reload", defer: true %>
- Build Scripts in
package.json
"scripts": {
"build": "esbuild app/javascript/*.* --bundle --sourcemap --format=esm --outdir=app/assets/builds --public-path=/assets",
"build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
}
- Assets Folder Configuration
I verified that the compiled files are being created correctly in app/assets/builds
:
ls -l app/assets/builds
# Output:
# -rw-r--r-- application.css
# -rw-r--r-- application.js
# -rw-r--r-- application.js.map
- Updated the Manifest
In app/assets/config/manifest.js
, I ensured the builds folder is linked:
//= link_tree ../images
//= link_tree ../builds
- Added Asset Path to
config/application.rb
config.assets.paths << Rails.root.join("app/assets/builds")
- Ensured Public File Server Is Enabled
In config/environments/development.rb
:
config.public_file_server.enabled = true
The Outcome
Even with all these changes, the browser still throws the same error:
No route matches [GET] "/assets/application.css"
No route matches [GET] "/assets/application.js"
I’ve verified the files are being built correctly, so the issue seems to lie in how Rails serves or routes these files. Restarting the server (bin/dev
) and clearing the cache made no difference.
Conclusion
Everything is set up as it should be, yet the assets refuse to load. I know the files are being generated in app/assets/builds
, but Rails still cannot find them. If anyone has run into this issue and found a solution, I’d love to hear from you!
Make sure you have the builds folder included in the asset pipeline:
# app/assets/config/manifest.js
//= link_tree ../builds
Thank you for your answer. I have included it as described above.
In app/assets/config/manifest.js
, I ensured the builds folder is linked:
//= link_tree ../images
//= link_tree ../builds