I'm trying to setup a staging environment.
I copied the environments/production.rb to create environments/staging.rb.
I tested the staging environment locally and ran
RAILS_ENV=staging rake assets:clean assets:precompile
RAILS_ENV=staging rails s
Assets are properly generated in public/assets, and when I load a page, the links to the assets are correct (eg: /assets/application-e014056a81279320a97c71ed25eb46ab.css)
But the browser can't load them and if I try to open
http://localhost:3000/assets/application-e014056a81279320a97c71ed25eb46ab.css
I get a 404.
Now the strange thing is that it works in production, with:
RAILS_ENV=production rake assets:clean assets:precompile
RAILS_ENV=production rails s
For both environments/staging.rb and environments/production.rb, the config is
config.serve_static_assets = false
config.assets.compile = false
config.assets.digest = true
And in application.rb
Bundler.require(*Rails.groups(assets: %w(development test)))
Do you have any idea where to look ? What else could differentiate the staging environment from the production environment ?
So, I had forgotten the role of the 'rails_12factor' gem.
I updated my Gemfile to
gem 'rails_12factor', group: [:staging, :production]
And everything works now.
Related
I'm using Rails 5.0
Since I've done this command-line : ENV=production rake assets:precompile
when i change CSS I can see them in local instantly.
I have to kill the server, do this command-line again (ENV=production rake assets:precompile) in order to see the change I made.
Thaks for your help.
Rails requires you to pre-compile assets (css,js,etc) in production after you change anything in assets. However in development assets are compiled live by default.
If you want to compile assets live in production (Not Recommended):
then change config.assets.compile=false in config/environments/production.rb to:
config.assets.compile = true
That is Rails should recompile the assets when it detects that a new version of the source assets is there.
On production, you usually want to set it to false and handle asset compilation during deployment. For this, you have to run
RAILS_ENV=production bin/rails assets:precompile
Usually, if you deploy using Capistrano, it takes care of that.
Note: In rails 4 or earlier it was RAILS_ENV=production bin/rake assets:precompile but in Rails 5.x rake is merged into rails so you must use RAILS_ENV=production bin/rails assets:precompile.
In config/environments/development.rb,
I had config.assets.debug set to false instead of true.
I have a project that works in the local development environment but breaks when it is uploaded to Heroku. When visiting my project on Heroku, I notice that I get 404 responses from the server saying that it could not find my css and js files. I have done some searching and found out that Heroku is not precompiling my assets. The project will work fine until Heroku puts my project to sleep. Upon waking the project in Heroku, the css and js are broken.
The project is using Rails 4.2.4, I have made sure to to include config.serve_static_assets = true in my config/application.rb and gem 'rails_12factor', group: :production in my Gemfile.
The css and js only breaks when Heroku puts the project to sleep due to inactivity. Does anyone know how to have Heroku automatically precompile assets when it is awaken from sleep?
I had similar issues before, my best bet was to precompile in local and then push to heroku. Configure your production.rb as follows:
config.serve_static_files = false
config.assets.compile = false
then in your console precompile as follows:
rake assets:precompile RAILS_ENV=production
This will precompile everything in public/assets commit your changes and push to heroku.
Also reset your assets cache for avoid any inconsistence:
rake assets:precompile RAILS_ENV=production
The above will force all your public/assets directory to rebuild when you run precompile command.
If your issue is with assets recompilation my answer should solve it, if you still have issues then you are doing something wrong or the issue does not have anything to do with assets precompilation.
We set the configuration values of above to false because now you are sending the precompiled files to the repo, so we do not serve static files nor fallback assets pipeline if something is missing, we are going everything in local.
Gemfile
gem 'rails_12factor', group: :production
application.rb
By default Rails 4 will not serve your assets. To enable this functionality you need to go into config/application.rb and add this line:
config.serve_static_assets = true
production.rb
config.serve_static_files = true
config.assets.compile = true
Command Line
bundle install
bundle exec rake assets:precompile RAILS_ENV=production
Make sure the images are in the /public folder.
Reference here
On every push to Heroku, all assets are precompiled, so not only assets that have changed. Asset precompilation thus takes more than 250 seconds. I tried all suggestions I found online, but I can't seem to fix it.
I have installed the rails_12factor gem as suggested by Heroku. Static assets are hosted on Cloudfront. We use Bower-Rails for front-end dependencies. Unicorn is the server on Heroku.
These are some settings in the production.rb environment:
config.serve_static_assets = true
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
config.assets.compile = true
config.static_cache_control = 'public, max-age=31536000'
config.assets.compress = true
config.assets.digest = true
Has anyone experienced this as well? Or can anyone help me?
Have you tried running this before pushing to heroku?
bundle exec rake assets:precompile RAILS_ENV=production
Then do a git commit and git push heroku master
I wanted to post this as a comment but didn't have the rep ><
Shows fine locally. But when I upload to Heroku, I get the following:
<%= image_tag('logo-red.png') %>
and it's located in assets/images/
I'm not using Turbolinks. Do I need to run a command on Heroku to solve this or is there some config setting I'm missing?
edit 1: tried running heroku run rake assets:precompile RAILS_ENV=production
edit 2: response from heroku staff:
It looks like your app is properly compiling that image:
~/public/assets $ pwd
/app/public/assets
~/public/assets $ ls | grep logo-red
logo-red-a07050d882e1dba431cef2130d39f929c611eaf8b0ec3c50db0742ddccb14d93.png
edit 3: See attached screenshot
Try running on your local computer:
rake assets:precompile
rake assets:clean
Then commit and push to heroku.
Also check your production.rb file and make sure everything related to compiling or precompiling is true and not false ie:
config.serve_static_assets = true
config.assets.compile = true
Also, make sure rails_12factor is in your gemfile like so:
gem 'rails_12factor', group: :production
Had this problem on Heroku before - we solved it by precompiling the assets on Heroku itself:
$ heroku run rake assets:precompile RAILS_ENV=production
I know this is done when you push the repo to Heroku; it's one of those quirks which seems to be resolved if you compile the assets on their server.
You could also precompile locally as long as you make sure the RAILS_ENV is production:
$ rake assets:precompile RAILS_ENV=production
After below answers if your problem still continuing
you ca try:
<%= asset_path 'logo-red.png' %>
I used the gem asset_sync and aws to precompile my assets. rake assets:precompile works fine. After I pushed my app to heroku, and
heroku run rake db:migrate
I get the following error
"uninitialized constant AssetSync"
initializers/asset_sync.rb
AssetSync.configure do |config|
config.fog_provider = 'AWS'
config.aws_access_key_id = "..."
config.aws_secret_access_key = "..."
config.fog_directory = Rails.env + "-..."
config.fog_region = 'eu-west-1'
end
config/production.rb
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY']}.s3.amazonaws.com"
config.assets.enabled = true
After running run rake assets:precompile the first time, all my app/assets/images were moved to public/assets . I've deleted them from github and added public/assets/* to .gitignore. May this be the problem?
Edit: when running git push heroku master, it looks like they were precompiled
Preparing app for Rails asset pipeline
Running: rake assets:precompile
AssetSync: using /tmp/build_2ltvklj0gaxjp/config/initializers/asset_sync.rb
AssetSync: using /tmp/build_2ltvklj0gaxjp/config/initializers/asset_sync.rb
AssetSync: Syncing.
Using: Directory Search of /tmp/build_2ltvklj0gaxjp/public/assets
Uploading: assets/application-7e17d9f0ed9cb7ea50b750e2bfc7e28c.css.gz
Uploading: assets/application-7e17d9f0ed9cb7ea50b750e2bfc7e28c.css
AssetSync: Done.
Asset precompilation completed (58.04s)
Your initializer assumes that AssetSync is always defined, but this will not be the case if your Gemfile looks like:
group :assets do
gem 'asset_sync'
end
The asset_sync documentation recommends wrapping the initializer in:
if defined?(AssetSync)
...
end
This is because Heroku runs production without the assets group of gems. Heroku precompiles your assets when you run a push -- and if asset_sync is enabled, it will update S3 at that time -- so when your application launches later, it no longer needs those gems. Thus, your asset_sync initializer needs to handle the situation where the gem is not loaded.