I recently deployed a first iteration of a project to Heroku. Because I precompiled, all of my assets used during development are now in the public/assets and public/system folders.
I am aware of two options for accessing my assets during development. The default option seems to be to allow the public/assets files to override my app/assets files. However, if I do this, any CSS changes I make in app/assets is not reflected.
The alternative option is to access ONLY the app/assets folder through:
config.serve_static_assets = false
However, by doing this, I can't see any of my images during development, as they have already been precompiled and moved to public/system
Is there a way to access my CSS/JS files from app/assets, yet still load my images from public/system?
Or am I supposed to do all of my CSS/JS development out of the public/assets folder? Any feedback would be much appreciated.
After a deploy I like to run
rake assets:clean
to get rid of the compiled/compressed assets in public/assets. This let's Rails look back to my app/assets directory in development mode since none of my assets are in public/assets anymore.
Also, unless your circumstances are special, you should be putting your images into app/assets/images, not public/system. rake assets:precompile pushes images into public/assets too. When doing this, you need to use image_path(...) within your ___.css.scss file(s) to allow the paths to be updated properly with the hashed filename generated for the image assets during precompile.
Finally, it seems reading through this Rails Guide would do you a lot of good.
Related
I have a relatively simple Rails application which has a few coffeescript files that I need to change occasionally.
The public assets folder keeps growing with new files every time I am forced to recompile in order to push to heroku. When this happens I have new asset file that I have to add to git in order for them to be pushed to Heroku for my JS and CSS to appear (if I do not add these new files and push I am left with no CSS or JS).
Obviously I have got the wrong end of the stick here somehow, and am creating extra unnecessary files.
Can anyone shed light on this? I am including a screenshot of my public/assets directory so you get the picture:
Before you run rake assets:precompile, clean up your old assets:
with either rake assets:clean
which only removes old assets (keeps the most recent 3 copies) from public/assets. Useful when doing rolling deploys that may still be serving old assets while the new ones are being compiled.
or rake assets:clobber
which empties out public/assets and clears the Sprockets file system cache.
Source: https://github.com/rails/sprockets-rails/blob/master/README.md
Note that rake assets:clobber is for Rails 4 only.
I precompiled assets on my dev environment (by mistake!) now any changes done on js/css files are not reflecting on browsing site locally.
I removed assets folder from public directory but then no css/js was available.
How do I get rid of this?
As a temporary solution I just cloned project into new directory and it works.
The question is: why do you need asset precompilation in the development environment? It's not meant to work like this.
The asset pipeline allows working in development with the uncompressed, unminified versions of your JS files. It also reloads them each time you refresh your browser, so you can develop your application with ease.
In production, though, the asset pipeline precompiles the JS files / assets you have into one single, minified file. This allows for better performance on the client as the files are smaller and are fetched in one single request.
So precompiling assets in development makes no sense at all.
In case if I understood you correctly, generally, when you precompile by default assets directory is being created inside public directory. To get your assets back, you can precompile again.
There is also a cache in tmp directory that you might consider removing.
Later you could use a $ (bundle exec) rake assets:precompile in combination with $ (bundle exec) rake assets:clean instead of $ rm -r public/assets so that new assets would be in effect rake way.
A one line command to look at new changes after your commits in environment would be
$ RAILS_ENV=(environment) rake assets:clean assets:precompile
but generally in development assets are not meant to be served as in production mode, so running previous with RAILS_ENV=production and starting a local server in production mode would be considered as a way to check (but not to make sure) if your assets would be served upon deployment in real production.
I went into the exact same problem and resolved it following #dashi's advice: 'remove directory assets in public, then start server in development mode.' everything is back to normal.
I have a Rails 3.2 app that has a lot of assets. That's OK because I'm using the [Asset Sync Gem(https://github.com/rumblelabs/asset_sync) which pushes all my compiled assets to an S3 bucket.
Problem is that these assets are still included in the slug size even though the application uses the versions on S3. This is causing my slug size to end up going over the 300MB limit.
I can't use a .slugignore file as this would prevent the files from being synced, so how can I delete all the assets once AssetSync has pushed them to S3 and before the slug is compiled?
[UPDATE]
I now precompile everything locally. Heroku is super-slow at compiling assets and more importantly, it compiles everything every time. Once your assets reach a non trivial size, this is really painful. I now precompile locally and have a Rake task that checks in the manifest and pushes to Heroku.
You can add behavior that deletes the directory after assets:clean is run
require 'fileutils'
Rake::Task["assets:clean"].enhance do
FileUtils.remove_dir "#{Rails.root}/public/assets", true
end
You can put that in your Rakefile.
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
I am developing an application with Rails 3.0 and Backbone and I tried
asset precompilation (rake assets:precompile).
Since then any change I made in the code is not reflected in the executed application,
in development environment.
Thank in advance
You'll have to pre-compile assets every time you make a change.
rake assets:precompile RAILS_ENV=development
I proposed a possible cause and solution to a similar question here which relates to
config/application.rb containing files to be precompiled.
I am writing with respect to Rails 3.2.22
if you are facing this problem then here is solution:-
Causes
Since you are run rake assets:precompile the script has created a folder public/assets and generated all the assets file the browser might ask for. So, when you make new changes in the js/css asset files, the requests from browser are served from public/assets directory.
Two Solutions
rm -df public/assets
rake assets:clean