Assets of LocomotiveCMS App - heroku

I have hosted a LocomotiveCMS app on Heroku. I've thereafter been working directly on the Heroku MongoHQ database, and have completed the wesbite.
I've then set the development database on my local machine to connect to the MongoHQ database used by the HerokuApp, so the local machine uses the actual data that's on the web. But the assets (CSS and JS files ) donot get accessed properly. Even though I've set up AWS Credentials.
So, when I access the CSSS+JS files on my local machine, I get the files, but with no content, i.e. blank file. I also dont get the AWS links for the assets, as I was seeing on the Heroku server.
Is it something to do with asset precompilation. I have set up the Heroku exactly as described here: http://doc.locomotivecms.com/guides/hosting/heroku-hosting
The production.rb file is as follows:
Testapp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# 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
# Defaults to nil and saved in location specified by config.assets.prefix
# config.assets.manifest = YOUR_PATH
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
# config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true
# See everything in the log (default is :info)
# config.log_level = :debug
# Prepend all log lines with the following tags
# config.log_tags = [ :subdomain, :uuid ]
# Use a different logger for distributed setups
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
# Use a different cache store in production
# config.cache_store = :mem_cache_store
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
# Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
# config.assets.precompile += %w( search.js )
# Disable delivery errors, bad email addresses will be ignored
# config.action_mailer.raise_delivery_errors = false
# Enable threaded mode
# config.threadsafe!
# Enable locale fallbacks for I18n (makes lookups for any locale fall back to
# the I18n.default_locale when a translation can not be found)
config.i18n.fallbacks = true
# Send deprecation notices to registered listeners
config.active_support.deprecation = :notify
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => 25,
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => ENV['SENDGRID_DOMAIN']
}
end

Run this in your console
bundle exec rake assets:precompile
then push to git, then to heroku

I understand why you have set the following to config.assets.initialize_on_precompile = false this makes your application run faster. However the ruby on rails guides states the following:
If you set config.assets.initialize_on_precompile to false, be sure to test rake assets:precompile locally before deploying. It may expose bugs where your assets reference application objects or methods, since those are still in scope in development mode regardless of the value of this flag.
So potentially this could be the route of your problem. Also if you want to include additional css and js you can add the following into the precompile array like so:
config.assets.precompile += ['admin.js', 'admin.css']
See following link for additional reading:
Precompiling assets
Hope this helps

Related

Carrierwave with S3 and Cloudfront not displaying images

Hello I am currently building a site using Rails and Heroku as well as GoDaddy for a custom domain name.
I am using Carrierwave with Fog, Amazon S3, and have set up a cloudfront with my Heroku domain name as the Origin Domain. Currently, the site loads up images very slow which is why I set up a cloudfront, but my carrierwave's config file doesn't seem to change the urls to the cloudfront counterparts. Here is my carrierwave config file. I would like users to be able to upload images through Carrierwave and then have those served back with my cloudfront.
CarrierWave.configure do |config|
config.fog_provider = 'fog/aws' # required
config.fog_credentials = {
provider: 'AWS', # required
aws_access_key_id: 'acesskey', # required
aws_secret_access_key: 'secretkey', # required
region: 'us-east-1', # optional, defaults to 'us-east-1'
}
config.fog_directory = 'directory' # required
# config.fog_public = false # optional, defaults to true
config.asset_host = 'randomjunk.cloudfront.net'
config.fog_public = false
config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" } # optional, defaults to {}
end
It works as long as config.fog_public is false but when I change it to config.fog_public = true the urls are now correct but the images are not found. Is this something to do with my configuration settings or have I set up my cloudfront with Heroku incorrectly?
It depends on the settings that were used when you created the files initially. If they were created with config.fog_public = false they will be private files and access will be done with a special signed url to s3. I think the asset host is just used for files that are public (hence what you are seeing). If you upload files with fog_public = true then I don't think you would see the not found. You may also be able to just set it to true and re-save images to change that attribute (I'm less sure about specifics here, you certainly should be able to change them to public though).

Rails 3.2 Assets in Production

I'm at my wits end with this. It seems all of the newer rails apps I make its set a couple of configs in environments/production.rb, deploy and move on with my life. But now we're migrating a few rails apps to a new server and it seems all of them have this issue when deploying to production.
What appears to be happening is that neither my javascripts or stylesheets are getting compiled. And I see none of the styles for the app and the javascript does not work.
config/application.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(:default, :assets, Rails.env)
end
module MyApp
class Application < Rails::Application
... omitted code ...
# Enable the asset pipeline
config.assets.enabled = true
# Version of your assets, change this if you want to expire all your assets
config.assets.version = '1.0'
config.generators.stylesheet_engine = :scss
end
end
config/environments/production.rb:
MyApp::Application.configure do
# Code is not reloaded between requests
config.cache_classes = true
# Full error reports are disabled and caching is turned on
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
# Disable Rails's static asset server (Apache or nginx will already do this)
config.serve_static_assets = false
# Compress JavaScripts and CSS
config.assets.compress = true
# Don't fallback to assets pipeline if a precompiled asset is missed
config.assets.compile = true
# Generate digests for assets URLs
config.assets.digest = true
... omitted code ...
end
In my application-<...>.js:
// This is a manifest file that'll be compiled into including all the files listed below.
// Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
// be included in the compiled file accessible from http://example.com/assets/application.js
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// the compiled file.
//
//= require jquery
//= require jquery_ujs
//= require_tree
;
And my application-<...>.css is completely empty. What am I missing?
So after a long half day yesterday and about an hour today, I suddenly realized something... I ran into this before.
Sure enough... Updating rails to 3.2.14 and deploying again fixed the whole thing.

Rails 4 assets not being served in production on Windows

I'm currently having the exact same problem as described in this post: Rails not serving assets in production or staging environments.
I am running Rails 4.0.4 in production environment on Windows 7 (so that could easily be the problem, can't use Linux unfortunately). I have run rake assets:clobber to make sure everything is cleaned up and and then RAILS_ENV=production rake assets:precompile and it succeeds without errors or warnings. All the files appear in my public/assets folder and using Windows explorer I can view the text in application.js and application.css, and images display correctly. However when I try to visit localhost:3001/assets/application.js it is blank, same with application.css, and image files come up with an error. I have restarted the server each time after changing settings and precompiling.
When I look at the logs it says the page renders successfully, there are no "No route matches" errors like I have seen in other posts. So the assets are being found, but for some reason they aren't being properly served.
Here is my production.rb:
ABC::Application.configure do
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.action_dispatch.x_sendfile_header = "X-Sendfile"
config.serve_static_assets = true
config.action_mailer.default_url_options = { :host => '' }
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
config.assets.compile = false
config.assets.digest = true
end
Any help would be much appreciated, I've been stuck on this for nearly two days!
Just as I finished typing up this question, I finally came across another post which said the headers were being set but there was no body, which sounded like my problem. They were using nginx and fixed the problem by changing the following:
# Specifies the header that your server uses for sending files
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
And seeing as I'm not using either Apache or nginx, I commented out both lines and finally my assets were served. Seems obvious in hindsight but I thought I would post this anyway in case it helps anyone else having the same problem.

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.

Rails asset pipeline not working in production environment?

I have recently upgraded an app from Rails 3.0 to 3.1. I have followed any instructions I could find for enabling the asset pipeline but it always fails when in the production environment:
<%= javascript_include_tag "application" %>
gives me
<script src="/javascripts/application.js" type="text/javascript"></script>
which is missing a digest and I get the following error:
cache: [GET /javascripts/application.js] miss
Started GET "/javascripts/application.js" for 127.0.0.1 at 2011-10-03 23:31:36 +0100
ActionController::RoutingError (No route matches [GET] "/javascripts/application.js"):
I've tried variations of these settings in application.rb:
require File.expand_path('../boot', __FILE__)
#require 'rails/all'
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module Blog
class Application < Rails::Application
config.autoload_paths += %W(#{config.root}/lib)
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.assets.enabled = true
config.assets.version = '1.0'
end
end
and full production.rb (minus some comments)
Blog::Application.configure do
config.cache_classes = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = false
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
I have ran the rake assets:precompile task.
Am I missing any obvious steps?
Edit: Some additional details:
My assets are in app/assets folder. app/assets/images, app/assets/javascripts, app/assets/stylesheets, etc.
I see my files generated in my public/assets directory with names and digests.
app/assets/javascripts/application.js does indeed compile to something like public/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js They end up in the public directory.
Sprockets is not getting loaded.
In an effort to remove active record in a previous version of rails (a la this question Remove ActiveRecord in Rails 3 (beta)) the require 'rails/all' was replaced by
require "action_controller/railtie"
require "action_mailer/railtie"
require "active_resource/railtie"
require "rails/test_unit/railtie"
What was missing here was sprockets/railtie
See the Upgrading to Rails 3.1 Railscast
Make sure your assets are in app/assets folder. app/assets/images, app/assets/javascripts, app/assets/stylesheets, etc.
Execute rake assets:precompile
You should see files generated in your app/public/assets directory with names and digests if enabled.
app/assets/javascripts/application.js would compile to /assets/application-6ec417a53cb2bdb949966a153a61e7b1.js
If the asset is named similar to above with a digest, Production.rb should have the following config:
# Generate digests for assets URLs
config.assets.digest = true
If you look at the web page source you should see something similar to the following:
<script src="/assets/application-6ec417a53cb2bdb949966a153a61e7b1.js" type="text/javascript"></script>
Try to manually load the file by going to http://example.com//assets/application-6ec417a53cb2bdb949966a153a61e7b1.js
The file should load, if not try checking permissions and further logs.

Resources