How to update CDN from rails app - ruby

When i run the command rake assets:precomplie for production evn. then assets is updating on locally but did not update on cloudfront cdn files like (http:///assets/pages/!-cc5be2564b7a1c153e5f74c677795613.js). old assets is serving but not new updated assets serving. can you please provide any help to me that how can we update cdn assets.

Did you set the config.action_controller.asset_host directive to your CDN url?
Make sure the RAILS_ENV environment variable is properly set to production. You can also prepend it to the full command: RAILS_ENV=production bundle exec rake assets:precompile.

Within config/environments/production.rb you will see a setting asset_host that is responsible for prepending a domain onto all asset links created via the built in asset helpers
# config/environments/production.rb
config.action_controller.asset_host = "<YOUR DISTRIBUTION SUBDOMAIN>.cloudfront.net"
Once deployed, this application will now serve all assets through the CloudFront distribution.
For more details : https://devcenter.heroku.com/articles/using-amazon-cloudfront-cdn
One more thing:
If you want to precompile your assets in production mode then check in production.rb:
config.serve_static_assets = true
config.assets.digest = true
config.assets.compile = true # make it false if you don't want to let heroku precompile everytime
And then Run this below in your terminal:
RAILS_ENV=production bundle exec rake assets:precompile
then deploy I hope this helps you :)

Related

Images not being served on Heroku Rails 5 App

I'm having issues serving static files from my Asset pipeline using Rails 5 on Heroku. I have looked at other questions but none seem to resolve the issue.
I get
HomeController#index is missing a template for this request format
and variant. request.formats: ["image/png"] request.variant: []
my config/production.rb
config.assets.compile = false
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
if ENV["RAILS_LOG_TO_STDOUT"].present?
logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger = ActiveSupport::TaggedLogging.new(logger)
end
Compile all assets locally and push to heroku.
Try following steps:
change line in config/environments/production.rb
config.assets.compile = true
Run commands:
RAILS_ENV=production rake assets:precompile
push all compile assets with manifest.rb to heroku.
Compiling assets locally is not a good practice and should be avoided as artifacts should not be in version control. I was having the same issue while attempting to set a background image using sass and using the image-url helper generates the correct url.
background: image-url('my-image.png') no-repeat center;
For image tags and erb css templates there is the image_path and image_tag rails helpers.

Assets loading issue on Rails 5 app with Heroku

I am facing asset loading issue in Rails 5 application deployed on Heroku.
App Configuration is,
ruby => ‘2.3.1’
rails => '~> 5.0.1'
When image is stored on path,
app/assets/home/image1.jpg
I am accessing it in view as,
= image_tag('/assets/home/image1.jpg’)
which is working properly in Development ENV, but not in Production ENV.
As per Heroku log,
ActionController::RoutingError (No route matches [GET]
"/assets/home/image1.jpg")
If I am moving image directly to
app/assets/image1.jpg
then its working on Production ENV.
Please guide about it.
Thanks
It looks like you assets are not compile on heroku.
Follow below code:
config/environments/production.rb
config.assets.compile = true
then run commands:
RAILS_ENV=production rake assets:precompile
then push all compiled files with menifest file to heroku.

How disable assets compilation on heroku?

I'm trying to deploy my rails app to heroku using this turtorial:
https://devcenter.heroku.com/articles/getting-started-with-rails4
So, I use rails 4.1.1 and ruby 2.1.1
My Gemfile has gem 'rails_12factor', group: :production inside.
My application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
module Charticus
class Application Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
# config.time_zone = 'Central Time (US & Canada)'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
end
end
I created file public/assets/manifest.yml
But when I deploy app to heroku - it compile all my js-files files to application.js and all css-files application.css. And I can't see it on app.heroku.com using firebug.
What I need to do with my configurations to see all my js and css files on app.heroku.com ?
How disable assets precompiling and minification on heroku?
Help me please!
Thanks
lib/tasks/assets.rake
Rake::Task["assets:precompile"].clear
namespace :assets do
task 'precompile' do
puts "Not pre-compiling assets..."
end
end
You are done.
I compare config/environments/development.rb and config/environments/production.rb.
And make production.rb asset configs like in development.rb:
Comment this lines:
config.serve_static_assets = false
config.assets.js_compressor = :uglifier
config.assets.compile = false
config.assets.digest = true
Then:
Push my changes to git repo git push origin master
Push changes to heroku git push heroku master
Rails 4 applications have a manifest-*.json file, not a manifest.yml file. This file is typically generated when you run rake assets:precompile , how are you compiling your assets?
Regardless, you need a file public/assets/manifest-(fingerprint).json file
Fast forward to 2018, and you would need to add the following to config/initializers/production.rb:
config.assets.enabled = false
Then you'd need to customize Heroku's Ruby Buildpack to not run the assets:precompile rake task. I won't provide a link to such a buildpack because I won't support or warrant one, but its pretty easy to find it in lib/language_pack/ruby.rb and start removing relevant code.
You'd then have to configure your Heroku app to use your new forked Buildpack instead of the default one (e.g. using heroku buildpacks).
Thats the cleanest way to disable the asset pipeline in a Heroku app w/ Rails, without resorting to overriding Rails' built-in rake tasks.
Fast forward to 2021 and Rails 6.x, if you completely removed Webpacker and Sprockets/Asset Pipeline, replace the bin/yarn file content with something like:
#!/usr/bin/env ruby
puts 'Yarn not present, nothing to do.'
#danielricecodes's advice is probably still valid but way more invasive.

inconsistent asset precompiling Rails 4 and Heroku

Recently upgraded to Rails 4.0.2 from 3.2 on Heroku. I'm noticing that maybe every other push my stylesheet_link_tag and javascript_include_tag tags point to my development path (i.e. /assets/admin.css) instead of my production/precompiled # fingerprinted path such as /assets/admin-a334a2cf57ed6ffc29f7f7a1af35f380.css
here are the relevant setting from production.rb:
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = false
# Generate digests for assets URLs
config.assets.digest = true
Because I am on Heroku I have config.assets.initialize_on_precompile = false in application.rb. So I always run bundle exec rake assets:precompile before deploying if I have made any changes to asset files.
Here is my folder hierarchy:
app
-assets
-images
-javascripts
-stylesheets
-themes
dark.css
blue.css
etc...
admin.css
application.css
jobboard.css
here is my application.config
config.assets.precompile += [
'admin.css',
'admin.js',
'jobboard.js',
'jobboard.css',
'themes/dark.css',
'themes/blue.css',
'themes/green.css',
'themes/plain.css',
'themes/seafoam-flat.css',
'themes/fire-flat.css'
]
But for some reason I get this inconsistent behavior in production. All files precompile. But sometimes the admin.css file is not referenced w/ the fingerprint, same for the css files under /themes. Any clue as to why this might happen?
If there's a missing precompile file maybe you should add them in config/application.rb
config.assets.precompile += %w( admin.css )
and then
RAILS_ENV=production bundle exec rake assets:precompile
You can check in the manifest ( public/assets ) for missing file or to see the fingerprint.

no route matches for assets/images in Rails

Working on rails, images are not visible and giving error.
Started GET "/assets/home.png" for 127.0.0.1 at 2012-06-19 12:23:24 +0530
Served asset /home.png - 404 Not Found (24ms)
ActionController::RoutingError (No route matches [GET] "/assets/home.png"):
I have used command
rake assets:precompile
production.rb
config.assets.compress = true
config.assets.compile = false
application.rb
config.assets.enabled = true
config.assets.version = '1.0'
Thanks for any help!
Actually you cannot reference your image with /assets/home.png path.
It will work in development mode, but in production all of your assets have a fingerprint in their filename (read this http://guides.rubyonrails.org/asset_pipeline.html#what-is-fingerprinting-and-why-should-i-care-questionmark)
That's why, in assets-pipeline enabled applications you need to reference all of your assets using helper methods. Read this doc to learn about the different helpers available in Ruby, JS and Sass files: http://guides.rubyonrails.org/asset_pipeline.html#coding-links-to-assets
The lack of a fingerprint in the file request suggests that you are running this in development. I am also going to guess that this is an app upgraded from an older version of Rails.
Any images need to be in the folder /assets/images for the pipeline to work.
Also, you do not need to precompile when in development mode.
Delete the public/assets folder, delete the folder tmp/cache/assets, and restart your server.
If this images are in the correct location, it should work.

Resources