I have used the asset_sync gem many times before with great success, but using it in a Rails 4.0.3 project seems to have caused a problem.
The assets are uploaded, hashed and gzipped to the target directory (I just went with the default 'assets'), but when running the application in staging/production environment the paths are incorrect.
They take on the form:
S3_DOMAIN.com/stylesheets/application.css
Instead of:
S3_DOMAIN.com/assets/application-HASH.css
Has anyone else experienced this problem? The only way I have found to reverse this behaviour is to set config.assets.compile to true, but this won't do in a production environment.
Here are the relevant config files:
## environments/staging.rb
config.serve_static_assets = false
config.assets.compress = true
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
# Have to set this to true to make asset_sync generate the correct links
config.assets.compile = false
config.assets.digest = true
config.assets.enabled = true
config.assets.initialize_on_precompile = true
config.action_controller.asset_host = "//#{Figaro.env.fog_directory}.s3.amazonaws.com"
config.action_mailer.asset_host = "//#{Figaro.env.fog_directory}.s3.amazonaws.com"
config.assets.prefix = "/assets"
config.assets.debug = false
config.assets.cache_store = :memory_store
##config/asset_sync.yml
defaults: &defaults
fog_provider: 'AWS'
aws_access_key_id: "<%= ENV['AWS_ACCESS_KEY_ID'] %>"
aws_secret_access_key: "<%= ENV['AWS_SECRET_ACCESS_KEY'] %>"
# To use AWS reduced redundancy storage.
# aws_reduced_redundancy: true
# You may need to specify what region your storage bucket is in
fog_region: <%= ENV['FOG_REGION'] %>
existing_remote_files: keep
# To delete existing remote files.
# existing_remote_files: delete
# Automatically replace files with their equivalent gzip compressed version
gzip_compression: true
# Fail silently. Useful for environments such as Heroku
# fail_silently: true
development:
<<: *defaults
enabled: false
test:
<<: *defaults
enabled: false
staging:
<<: *defaults
fog_directory: <%= ENV['FOG_DIRECTORY'] %>
production:
<<: *defaults
fog_directory: <%= ENV['FOG_DIRECTORY'] %>
You need to run everything assets related in
production
mode in rails 4.for example:
If you run it in the default mode (development) the hash would be different so rails leaves off the hash all together.
Also, you will want to do this before you start the server so it finds the files.
NOTE: I think this change was to allow you to cache assets in development.
Hopefully this will help save my fellow programmer friends some head banging :D. I have answered this question on "digest_path & asset_digest_path not allowing digest URLs" but will repost it here so it will save you some clicks.
I was uploading the files to S3, I didn't realize that the manifest wasn't loaded by Rails. You can have all your production settings right (like above and in other threads), but if you don't have the manifest.json file readable by Rails, it will still generate /javascript/* (example) urls.
I was still having trouble with
multi_json
gem's latest version, so I downgraded it to 1.7.8 and it works fine.That's so it can read the
manifest.json
file whichrake assets:precompile
creates.There is a debate on this sprockets thread https://github.com/rails/sprockets-rails/issues/107 on whether your manifest file should be in git or just on a deploy script, do what suits you best, just make sure it is findable in:
or specifiy it yourself with
That may or may not be depricated.
Cheers!