in my Rails application all my js and css is in public folder.
in dev mode it works fine. but when I switch to production mode it dosn't work
no css and js is found.
what could be the problem?
You'll need to enable this behaviour for production. Within config/environments/production.rb, set the following
config.serve_static_assets = true
In your app/config/environments/production.rb, check if the following statement is present:
config.serve_static_assets = true
Related
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:
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)
Do I need to be manually precompiling these assets on deployment?
Help would be appreciated. Thanks!
#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.
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.
Setting the environment variables RAILS_ENV=production and RAILS_SERVE_STATIC_FILES=true did the job for me (with Rails 7.0.4).
I want to show unbundled javascript and css of my web-application to a UI developer. I have tried adding require 'sprockets/railties'
config.assets.debug = true
in my production.rb, but it did not work and I can still see bundled, uglified css/js in my browser sources.
I tried running my production in development mode by adding rack_env development in my /etc/nginx/nginx.conf http block, but I get Bad Request due to following error:-
invalid number of arguments in "rack_env" directive in /etc/nginx/nginx.conf:16
Please help
Did you try purging and recompiling your assets by any chance? Depending on your deploy method, production assets may not be recompiled on every deploy / application start.
rake assets:clean (rake assets:clobber) for Rails 4+
rake assets:precompile
By default, config.assets.debug = true is what controls this 'bundling' behavior.
You might also try to comment out
config.assets.js_compressor = ... or
config.assets.css_compressor = ...
if you have any of those in your production.rb.
Another reason may be any sort of external cache, depending on where you host your app: Cloudflare or Heroku Asset Pipeline Cache (those usually cache based on MD5 of your assets).
And the last but not the least is... browser cache, just in case :)
I have precompiled the assets in production but the application is not able to load the assets. My app is deployed on AWS EC2
I checked in the server in public/assets folder the application.css is present.
but still it says 404 error.
My production.rb configuration is
My nginx.conf settings are:
The 404 is based on that line in your config: config.serve_static_files = true. That might be the wrong name depending on your rails version. I believe that setting is named config.serve_static_assets in Rails prior to version 5.
If you don't have a server (nginx/etc) then you want that setting to be true. Otherwise you need to reconfigure your server to point at your /public directory, and make sure rake assets:precompile runs successfully.
I had the same problem
set config.assets.compile = True
It works for me.
The problem was my configuration in nginx of root path was wrong. so assets were not pointing to wrong path.
I changed in nginx.conf:
from root /home/deploy/appname/public
to root /home/deploy/appname/current/public
AND in production.rb
config.serve_static_assets = true to config.serve_static_assets = false
I did the same as you:
root /home/deploy/appname/public root => /home/deploy/appname/current/public
And that works for me. With the unique difference that i left the config/production.rb file like this:
config.public_file_server.enabled = true
config.assets.compile = true
I'm using Rails 5.
i'm working with some legacy code and i'm having problems with some assets available in dev and not test. When I export to PDF in dev everything works fine, but when I try in test I get this error:
"No such file or directory - /Users/me/current_projects/foo/public/assets/uri-checkbox-icon.css"
The file is there, but has a number after it like this: "uri-checkbox-icon-0db5de580292128824f2242038cb1"
Does anyone know why that would load in dev, but not test?
Try RAILS_ENV=test bundle exec rake assets:precompile and updating your environments/test.rb so that config.serve_static_assets = true. This should update your asset pipeline configs in that environment.
Do you have the same settings for config.assets.digest in development.rb and in test.rb ?
(see: http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care-questionmark)
"Fingerprinting" (adding the number after the name of an asset) is enabled by default for production and disabled for all other environments. You can enable or disable it in your configuration through the config.assets.digest.
I'm starting in rails and I've got an application that is working in dev (webrick).
The problem is: the precompiled CSS file is not found in production.
Now I host it on heroku and deploy it. I have a message saying that assets are precompiled (so it's not a compilation error) and everything is by-default for settings.
I've ls the /public/assets folder and everything is in there. I can even cat the application-*.css file and I get the full content I should have.
Now when I try to access the CSS file it gives me an error 404 (even tho it's an auto-generated css link usingĀ <%= stylesheet_link_tag "application", :media => "all" %>). So definetly it's not a problem that I did hardcode the CSS link.
I'm not exactly sure on what would be the next check to perform.
If you're curious on the output, it is currently publicly accessible here.
Try changing the configuration option config.serve_static_assets = false to config.serve_static_assets = true in your config/environments/production.rb if you haven't already done that.
The only thing that fixed it for me in Rails 4 was
config.assets.compile = true
in config/environments/production.rb
This would fall back to the assets pipeline if a precompiled asset is missed, according to the documentation.