skip to Main Content

Trying out rails 7, with tailwind and postcss.

I have a few stylesheets in assets/stylesheets, which I import through a file called imports.css in the same folder.

//imports.css
@import stylesheet1.css;
@import stylesheet2.css;

I then import that file in application.html.erb with the following:

//application.html.erb
<%= stylesheet_link_tag "application", "imports", "data-turbo-track": "reload" %>

On development everything works fine as intended, meshes well with tailwind. Importing works fine due to postcss. However on production (nginx, puma) it tries to pull in those files and fails. For every stylesheet I have, i see in the browser console:

GET https://mywebsite.com/assets/style/stylesheet1.css net::ERR_ABORTED 404 (Not Found)

I am trying to shift my brain over from webpacker in rails 6, not sure what I need to do here… some thoughts:

  1. Do i need to set it up so that the stylesheets get copied over to the public/assets folder or something? Is there a setting to do that in production.rb? (I feel like rails by default should be already doing this)

  2. Do I need to be manually precompiling these assets on deployment?

Help would be appreciated. Thanks!

3

Answers


  1. Do I need to be manually precompiling these assets on deployment?

    Yes. Since you’re not using a Node workflow now, instead relying on Sprockets and assets directly, you need the precompile step in Production.

    Login or Signup to reply.
  2. @stellenberger was right and deserves the credit. In my case their solution of enabling the public file server for static assets fixed this issue for me and immediately started serving the css assets from public. I was already precompiling, they just weren’t being served. I am using Rails docker containers on an EC2 in AWS behind an ALB, and there is no local NGINX so the default assumed is not correct for my scenario and needed to be changed.

    The config/environments/production.rb file already provides an environment variable that will turn on the public server if present, called RAILS_SERVE_STATIC_FILES so all you need to do is set that environment variable in your build or deploy process. Since it is a presence check, any value will be truthy as the var existence is all that is checked.

    Alternatively, you could also set

    # config/environments/production.rb
    config.public_file_server.enabled = true
    

    in the file instead of using the ENV var, but generally the ENV vars are better than hard-coded configs.

    In my case, I changed the default to

    # config/environments/production.rb
    config.public_file_server.enabled = ENV.fetch("RAILS_SERVE_STATIC_FILES") { true }
    

    that way my default is true and enabled without setting the var, but I can still use the var to disable it if desired.

    Login or Signup to reply.
  3. Setting the environment variables RAILS_ENV=production and RAILS_SERVE_STATIC_FILES=true did the job for me (with Rails 7.0.4).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search