Rails 7 app deployment on Fly.io - CSS and JS files in the public directory return 404 errors

I’m experiencing an issue while deploying a Rails 7 application on Fly.io. Some CSS and JS files within the public directory are not being served and return 404 errors in the browser console. However, JPG images within the same directory are being served without issues.

I’ve tried running rails assets:precompile, but it didn’t resolve the problem. The application works as expected in the local development environment, and there are no CSP or CORS errors in the console. The config.public_file_server.enabled setting in my project is set to ENV["RAILS_SERVE_STATIC_FILES"].present?.

I’ve also checked the VM instance on Fly.io using the web terminal, and it appears that the problematic CSS and JS files are not present in the /rails/public directory on the server.

I am deploying the app using the fly deploy command and not using GitHub. The fly.toml file doesn’t contain a [build] section. I have tried adding a [build] section with builder = "heroku/buildpacks:20" and setting the RAILS_ENV variable to production, but the issue persists.

Please advise on how to ensure that the CSS and JS files in the public directory are served correctly during deployment on Fly.io.

Rails’s asset pipeline is a complex subject. Unfortunately, without seeing your application it is hard to provide specific advice. Generally the place to start is to try to get your application working on your machine in production mode first, then try to deploy it:

bin/rails server -e production

Start by reviewing your manifest file, and then make sure you are using the css and js helpers.

Thank you for your suggestion. My application is running correctly in development mode but is experiencing issues with asset serving in production mode on Fly.io.

Funny thing I can acess all files on “public” root, but none on the sub-directories there

My suggestion is to try running in production mode on your machine, and get it working there, before running in production mode on Fly.io.

As an alternative, you can run in development mode on Fly.io. At the top of the Dockerfile, remove the “# Set production environment” section which sets RAILS_ENV, BUNDLE_WITHOUT, and BUNDLE_DEPLOYMENT. If you like, you can explicitly set RAILS_ENV to development.

You can also remove any reference to bootsnap and assets:precompile.

Depending on whether or not you are using a js or css framework, you may also need to change the final line from running ./bin/rails server to ./bin/rails dev.

Be aware that running in development mode will be slower and require more memory than running in production.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.