The problem with Rails Asset Pipeline that I always stumble upon
You know how it is, you develop using Rails and it is going great! Tailwind and downhill, you develop with an amazing speed. Then when teh app is starting to get really useful, you say: “hmm maybe I should run in in production mode just to get that out of the way. I remember there was a few snags but I have forgotten about then now. Better start so I can continue the real development as soon as possible.”
So you do your “bundle exec rake assets:precompile” and start the app with “RAILS_ENV=production bundle exec rails s” and expect maybe a few missing resources because of invalid hardcoded filename and such.
No such luck. Your site looks like crap, it looks like nothing has been loaded. JQuery and Bootstrap maybe, but the Theme you downloaded and put in /public is not loaded at all…
You do the normal shot-in-the-dark debugging (cleaning caches, restarting browser… heck even restarting computer) but nothing works.
You start reading about the asset pipeline, beginner level, since you understand that you have forgotten most of what you now are reading.
After a while, a Stack Overflow article hits you: https://stackoverflow.com/questions/21969549/rails-application-css-asset-not-found-in-production-mode
This is because in production mode, you are expected to let Apache or NginX serve your application, and they will serve your static assets very efficiently.
When you run “rails s”, you normally run the PUMA webserver, and it is instructed by /config/environments/production.rb to NOT load static assets:
This line in config/environments/production.rb (Rails 5.1.6 used here) is causing this:
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
So if you set this environment variable, suddenly you see it again
export RAILS_SERVE_STATIC_FILES=1 RAILS_ENV=production rails s -p 3001 -b 0.0.0.0
Before Rails 4, you specify config.serve_static_assets = false in the same way (Yet another thing the Rails team cant avoid touching… why cant they just leave things alone?)