rake assets:precompile currently spits everything into my public/assets directory when I was under the impression (after setting up S3) that it would push up to Amazon. I am utilizing asset_sync as outlined here
Currently I have this in my application.rb:
class Application < Rails::Application
config.assets.enabled = true
config.assets.digest = true
end
Then in my development.rb I have:
config.action_controller.asset_host = "//#{ENV['FOG_DIRECTORY_DEV']}.s3.amazonaws.com"
config.action_mailer.asset_host = "http://#{ENV['FOG_DIRECTORY_DEV']}.s3.amazonaws.com"
config.assets.initialize_on_precompile = false
What am I doing wrong here?
You must appreciate that asset_sync is there to sync your assets (not replace them)
Asset Sync
The gem itself will let Rails publish your assets "locally" (to /public/assets), and then it will essentially push them all to your S3 bucket, replicating them.
As described by the gem's documentation:
Synchronises Assets between Rails and S3.
Asset Sync is built to run with the new Rails Asset Pipeline feature
introduced in Rails 3.1. After you run bundle exec rake
assets:precompile your assets will be synchronised to your S3 bucket,
optionally deleting unused files and only uploading the files it needs
to.
--
Fix
In regards to your problem, I'm sure that by default, development assets are served dynamically - meaning that if you want to run them as static (precompiled), you'll have to tweak some of the settings which define this:
#config/environments/development.rb
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = false #true
This should enable you to use the precompiled assets locally (in development), which will in turn allow you to use S3
Production serves static assets by default, meaning the most applicable way to test asset_sync is literally by deploying to your production environment. However, you should be able to use the code above to get it to work in development, too
Related
I am developing Rails 5 application and use assets pipeline.
It work well in development mode, but if I try to run it in production mode, it can't load images & styles correctly.
I checked and found that it is because
config.assets.compile = false
in config/environments/production.rb
Unless I set it true, it doesn't work at all.
I know live compilation isn't good for production, what is solution?
There are two options related to serving assets within a Rails server:
Asset compilation
config.assets.compile = true
refers to asset compilation. That is, whether Rails should recompile the assets when it detects that a new version of the source assets is there. In development, you want to have it set to true, so that your styles get compiled when you edit the css files. With the next request, Rails will automatically recompile the assets. 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.
Asset serving
The second option related to assets is
config.public_file_server.enabled
This describes whether it is Rails that should serve the compiled files from the public/assets directory. In development, you want that, so it's true by default. In production, you usually don't want to fire up your web server to serve the logo image or a css file, so you probably compile the assets and then host them separately (for example, on a CDN like cloudfront). If you still want them to be served in production, you can launch Rails with:
RAILS_SERVE_STATIC_FILES=true RAILS_ENV=production bin/rails server
Precompile your assets first.
Run RAILS_ENV=production rake assets:precompile to generate your stylesheets and js files in your public directory.
I'm getting a 404 on all of the images included with jquery-ui-rails on Rails 4.0.1 after going production. It works fine in development environment. The site is looking for /assets/jquery-ui/ui-icons_222222_256x240.png, but only public/assets/jquery-ui/ui-icons_222222_256x240-890385424135de1513f00cbecfb7f990.png exists in the filesystem. How come production build IDs are not being appended?
I've also had this problem with some fonts. For the time being I've worked around it by just manually copying and pasting to the sought path.
First thing to try is precompiling assets specifically for the production environment:
RAILS_ENV=production rake assets:precompile
If that doesn't do anything, set the following in production.rb and precompile again
config.assets.precompile += ['*.js', '*.css']
config.assets.compile = true
Precompiling assets on the production server is very slow. So I have decided to recompile them on the development machine and upload assets to the Amazon S3 by jammit. Everything's done but I have some negative problems:
I have to include public/assets directory to git control. Because if public/assets directory is empty on the production server fails
If I precompile assets on the development machine application.js includes in the HTML as compressed and that way I have duplicated js code. Changing js doesn't make any effect because precompiled application.js interrupts this code.
That way my development process includes following steps:
Remove precompiled assets if I'm going to change js or css
Do some changes
Precompile assets
Upload assets to S3 by jammit-s3
Do commit and push my changes including assets to the git server
Deploy by capistrano
My questions are:
Is it possible to configure development environment don't include compressed application.js if I have it in public/assets directory?
Is it possible to configure production environment to work with empty public/assets directory? Assets will only be on the S3 server.
For question one I don't know a permanent solution other than running:
bundle exec rake assets:clean
before you switch back to development mode. I'd be interested to see if you can just ignore the assets in development without turning the entire assets pipeline off.
In production.rb there is an option for your second question:
# Enable serving of images, stylesheets, and JavaScripts from an asset server
config.action_controller.asset_host = "http://assets.example.com"
It should then ignore your assets directory since it relies on the remote host.
Hope that helps.
I resolved this problem by including assets dir in gitignore and exclude only one file - public/assets/manifest.yml and production server works correctly now, i.e. config.action_controller.asset_host = "http://assets.example.com" works. It requires only manifest.yml file
Right now, every time I am changing something in the assets, I have to delete the assets folder from the public directory and then run rake assets:precompile to take effect.
Is this something right or wrong so I should put it in a capistrano task to do it automatically?
For some reason, it doesn't compile automatically the assets in production and it throws errors if I don't do the above (or it doesn't take effect the changes if there is the files already). Is there something I should put in the environments/production.rb?
Also I don't understand what the following code in the production.rb does:
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
I tried false and true but I didn't understand the difference.
I'm a bit confused as at how it should work the workflow in production, if what I am doing is right and about the settings for the assets in production.
Capistrano has built-in support for precompiling assets during deployment. Just add this line to your deploy.rb file:
load "deploy/assets"
I've just upgraded my app on Heroku from Rails 3.0 to 3.1, and I'm trying to make the assets pipeline work. The main issue is that I can read from the heroku log the following kind of lines, for every asset:
2011-09-03T16:35:28+00:00 app[web.1]: cache: [GET /assets/border-a3c571a354b9381740db48aeebfaa63a.jpg] miss
If I understand the pipeline correctly, this should not be "miss" for every request I make from a browser, but it should be found in the cache.
Reading the Heroku docs you can find this explanation:
Rails 3.1 provides an assets:precompile rake task to allow the compilation penalty to be paid up front rather than each time the asset is requested. If this task exists in your app we will execute it when you push new code.
But how should that "assets:precompile" task be? I tried building a project with rails 3.1 from scratch to try to find out, but there is no such task in a bare project. Or am I missing something? How could I make that the assets are found in the cache? Maybe is just an issue with configuration.
These are the options of my production config file:
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = true # If I turn this off I get a 500 error and logs say that an asset isn't compiled
My application.rb has this line:
config.assets.enabled = true
Thanks a lot for your help!
Also, take a look at http://guides.rubyonrails.org/asset_pipeline.html#precompiling-assets
For faster asset precompiles, you can partially load your application
by setting config.assets.initialize_on_precompile to false in
config/application.rb, though in that case templates cannot see
application objects or methods. Heroku requires this to be false
I was wondering the same thing, but here's a tip to help figure out if your assets are live-compiling or not
run rake assets:precompile locally
make some changes to your css but do not rerun the rake task
git add, commit and push to heroku
If the changes you made in step 2 show up on heroku, then you know your app is live-compiling
Don't forget that you are now in charge of http caching since Varnish is no longer included on celadon, so you need to set up rack-cache and memcached yourself:
heroku doc on http caching
setup rack-cache with memcached on heroku
heroku docs on memcached
But yeah, I found this puzzling too
Can you try with config.serve_static_assets set to true and
config.action_dispatch.x_sendfile_header = "X-Sendfile"
added to your config/environments/production.rb file?
When you push your code to Heroku you should see the precompiling announced by the slug compiler AFAICT.
Make sure you are on the Heroku "Cedar" stack. Then Heroku will automatically precompile your assets during slug compilation.
Note: I'm still getting "cache misses" too, but I don't think that's really true because your app wouldn't work if your assets weren't compiled.