Report this

What is the reason for this report?

Rails App with webpacker Assets not found on droplet

Posted on February 5, 2022

So I deployed my RoR app on a droplet, which works find locally, the location of my application.js and application.scss is the following “app/javascript/packs/” Both are located here. But on my droplet, css and js is not working with the following error in the console of dev tools:

“Failed to load resource: the server responded with a status of 404 ()” “Failed to load resource: the server responded with a status of 404 ()”

“GET https://my_app/packs/css/application-988c2e01.css net::ERR_ABORTED 404” “GET https://my_app/packs/js/application-b5d6631ec473acfb5df1.js net::ERR_ABORTED 404”

Any idea on why is this happening and should I do to solve this.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hey!

The issue you’re facing with assets not being found in your Rails app on the droplet, while everything works fine locally, is a common issue related to asset compilation and serving in the production environment. Here are several steps and checks to ensure your assets are correctly compiled and served:

  1. *: In the production environment, Rails doesn’t compile assets on the fly like it does in development. You need to precompile your assets. Run the following command in your app’s root directory on your droplet:

    RAILS_ENV=production bundle exec rails assets:precompile
    

    This command compiles all your assets, including JavaScript and CSS, and places them in the public/assets or public/packs directory, depending on whether you are using the webpacker or sprockets.

  2. After precompilation, ensure that the public/packs directory contains your compiled assets. If the assets are not there, the precompilation might have failed, and you’ll need to check the logs for any errors.

  3. If you’re using Webpacker, check your config/webpacker.yml file. Ensure that the settings under the production section are correct. Particularly, look for the public_output_path setting; it should typically be set to packs.

  4. If you’re using an asset host or CDN, ensure that the config.action_controller.asset_host in your config/environments/production.rb is set correctly. If you’re not using an asset host, ensure this is either commented out or set appropriately.

  5. Make sure that webpacker is installed correctly on the production server. You can run bundle exec rails webpacker:install to ensure it’s set up properly. However, be careful as this might overwrite some custom configurations.

  6. After precompiling your assets, restart your Rails server to ensure it picks up the changes:

    sudo systemctl restart your-rails-app-service
    

    Replace your-rails-app-service with the actual service name if you’re using a system service to manage your Rails app. If not, just stop and start the Rails server manually.

  7. If you’re using Nginx or Apache as a reverse proxy, make sure that the configuration is correctly serving your assets. For Nginx, you should have a location block that looks something like this:

    location ~ ^/(assets|packs)/ {
      root /path/to/your/app/public;
      gzip_static on;
      expires max;
      add_header Cache-Control public;
    }
    

    Adjust the /path/to/your/app/public to the actual path of your app’s public directory.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.