Rails 4 - production images and fonts not pointing to fingerprint - ruby-on-rails

I imported a custom theme into my Rails app for the home page. I've copied the entire folder into app/assets/customlibrary so that I don't have to split up the theme's files.
When I run my app in the production environment, Rails isn't able to find the fingerprinted assets in app/assets/customlibrary/* when they are referenced in
app/assets/customlibrary/css/style.css.
For example in style.css, background: url("../images/hero-image.jpg") results in GET http://localhost:3000/assets/images/hero-image.jpg 404 (Not Found)
I can't figure out how to fix this. Any advice?
folder structure
app/assets/customlibary/css/*
app/assets/customlibary/css/style.css
app/assets/customlibary/js/*
app/assets/customlibary/fonts/*.(svg|eot|woff|tff|)
app/assets/customlibary/images/*
app/assets/customlibary/images/hero-image.jpg
app/assets/stylesheets/themes/theme.css'
app/assets/stylesheets/style1.css'
app/assets/stylesheets/style2.css'
application.rb
config.active_support.escape_html_entities_in_json = true
config.filter_parameters += [:password]
config.encoding = "utf-8"
# setup bower components folder for lookup
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components')
config.assets.paths << Rails.root.join('vendor', 'assets', 'bower_components', 'bootstrap-sass-official', 'assets', 'fonts')
config.assets.paths << Rails.root.join('app', 'assets', 'customlibrary')
# customlibrary assets
config.assets.precompile += %w( css/* fonts/* images/* js/* )
# normal stuff
config.assets.precompile << /\.(?:svg|eot|woff|ttf)$/
config.assets.precompile << /\.(?:png|jpg)$/
config.assets.precompile += %w( base.css )
config.assets.precompile += ['themes/theme.css']
config.assets.precompile += ['style1.css', style2.css', ... ]
production.rb
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_assets = true
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
config.assets.compile = false
config.assets.digest = true
config.assets.version = '1.0'
config.log_level = :debug

Adding a path in config.assets.paths does not mean it makes the asset available from the web root of your application. (Which is my_app/public)
It adds a directory to the lookup path used by Sprockets and the asset helpers. When linking to assets from your stylesheets you can change the extension to .css.erb and use interpolation:
.class { background-image: url(<%= image_path 'image.png' %>) }
If you are using SASS, sass-rails maps the Rails asset helpers to SASS functions so you can just use:
.class { background-image: image-url("image.png") }
This does not require you to rename the file.

Related

Rails won't precompile the assets-fonts folder

I kept getting a 404 on my application, only to realize that it is looking for a font: /assets/fonts/footable.woff
So I figured I'd just create the fonts folder within my app/assets folder, and then re-run RAILS_ENV=production rake assets:precompile
I did this, and when I do an ls to public/assets/, there is still no fonts folder.
Here's my config/application.rb
module MyRailsApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
# 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.
config.time_zone = "Central Time (US & Canada)"
config.active_record.default_timezone = :local # Or :utc
config.eager_load_paths += Dir["#{config.root}/lib/custom_rb/**/"]
config.assets.enabled = true
config.assets.paths << Rails.root.join("app", "assets", "images", "iCheck")
config.assets.paths << Rails.root.join("app", "assets", "fonts")
# For sidekiq
#config.active_job.queue_adapter = :delayed_job
config.active_job.queue_adapter = :sidekiq
end
end
and here's my config/initializers/assets.rb file:
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path.
# Rails.application.config.assets.paths << Emoji.images_path
# Add Yarn node_modules folder to the asset load path.
Rails.application.config.assets.paths << Rails.root.join('node_modules')
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in the app/assets
# folder are already added.
# Rails.application.config.assets.precompile += %w( admin.js admin.css )
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
I have also restarted apache, and it's still not there.
What am I doing wrong?

Rails 5 Assets Not Loading in Production

I recently updated a few packages in my Rails application and now my assets aren't being served. Instead, I get the following error:
Failed to load resource: the server responded with a status of 404 (Not Found)
My assets are precompiled:
assets.rb
# Be sure to restart your server when you modify this file.
# Version of your assets, change this if you want to expire all your assets.
Rails.application.config.assets.version = '1.0'
# Add additional assets to the asset load path
# Rails.application.config.assets.paths << Emoji.images_path
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
# Rails.application.config.assets.precompile += %w( search.js )
Rails.application.config.assets.precompile += %w(ckeditor/*)
Rails.application.config.assets.precompile += %w(ckeditor/config.js)
Rails.application.config.assets.precompile += %w( *.js ^[^_]*.css *.css.erb )
application.rb
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module DeployTest
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.
config.assets.precompile += Ckeditor.assets
config.assets.precompile += %w( ckeditor/* )
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
config.active_record.default_timezone = :local
config.time_zone = 'Eastern Time (US & Canada)'
end
end
Before telling me to turn on compiling my assets, please understand that this is
a horrible idea. Thank you for any advice
UPDATE: I got it to work by adding:
config.assets.digest = true
to my config/environments/staging.rb file. Weird how I didn't need it before
At times you need to add both of these configurations in staging.rb or whichever environment you want the changes to reflect on.
config.assets.compile = true #set to false by default
config.assets.digest = true

rails assets urls don't have digest when calling from css.scss

I deployed my project to production and some of my assets don't work.
Those assets, which I use in html.erb with
<%= image_tag('outstaffing/outstaff2.svg') %>
work normally and return
<img src="/assets/outstaffing/outstaff2-eade8e303c63a428e7430e84866b732dae91cda0639d8d3d422c2ee86fb254aa.svg" alt="Outstaff2 eade8e303c63a428e7430e84866b732dae91cda0639d8d3d422c2ee86fb254aa">
However fonts and some images don't.
I have app/assets/fonts/RobotoCondensedRegular.ttf and app/assets/images/common/footer.svg which I try to call from css files.
style.css.scss
#font-face {
font-family: 'RobotoRegular';
src: url(font-path('RobotoCondensedRegular.ttf')) format('truetype');
}
#some_div{
background: asset-url('common/footer.svg');
}
After precompilling I have public/assets/styles-h5digest.css with
#font-face{
font-family:'RobotoRegular';
src:url("/assets/RobotoCondensedRegular-4a7c36df4318fee50a8159c3a0ebde4572abab65447ae4a651c2fe87212302b5.ttf") format("truetype")
}
This should work but returns me errors:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Server is trying to load assets/fonts/RobotoCondensedRegular.ttf but file name should be with digest. The same thing is with footer image.
I have already tried to change assets.rb, production.rb and application.rb in many ways. Moreover, I tried different extensions of my css files (css, css.scss, scss, css.erb) and helpers (asset-url, image-url, asset-data-url, asset_data_path, <%= asset_path() %>).
I am using Rails 4.2.0 and ruby 2.2.3.
My production.rb
config.serve_static_files = true
config.assets.js_compressor = :uglifier
config.assets.css_compressor = :sass
config.assets.compile = false
config.assets.digest = true
My application.rb
config.assets.enabled = true
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')
config.assets.paths << Rails.root.join('app', 'assets', 'images', 'img', 'main')
My assets.rb
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf|jpg|png)\z/
Rails.application.config.assets.precompile += ['styles.css', 'careers.css', 'team.css', 'home.css', 'services.css', 'contacts.css']
Error from log:
"GET /assets/fonts/RobotoCondensedBold.ttf HTTP/1.1" 500 1812 "http://...ip..../assets/application-6b568a81a81290ff96d145fa1f76bbf33919b85dad0f4235d8bf8759787a5076.css"
I think problem is in my server (apache) but i have no solution.
I added these two gems to my Gemfile:
gem 'rails_serve_static_assets'
gem 'rails_stdout_logging'
And set in my production.rb
config.serve_static_files = false
Now all fonts and images are working good.

Rails Engine assests are not precompiled

I am studying rails and using the engines with rails.
In production mode, rails did not load compiled assets of engines,
although I have executed:
$ RAILS_ENV=production bundle exec rake assets:clean assets:precompile
Please help if anyone knows the issue.
My settings are as below:
environment/production.rb
config.cache_classes = true
config.eager_load = true
config.consider_all_requests_local = false
config.action_controller.perform_caching = true
config.serve_static_files = false
config.assets.compile = false
config.assets.digest = true
config.log_level = :debug
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
config.log_formatter = ::Logger::Formatter.new`
engine/xxx/lib/xxx/engine.rb
Engines's option is -- mountable
module Moderna
class Engine < ::Rails::Engine
isolate_namespace xxx
# parent company asset precompile
initializer "xxx.assets.precompile" do |app|
app.config.assets.paths << Rails.root.join("app", "assets", "fonts")
app.config.assets.precompile << %w(
xxx/*.css xxx/fancybox/*.css xxx/skin/*.css xxx/google-code-prettify/*.css
xxx/*.js xxx/flexslider/*.js xxx/google-code-prettify/*.js
xxx/portfolio/*.js xxx/quicksand/*.js
xxx/*.svg xxx/*.eot xxx/*.woff xxx/*.ttf
xxx/customicon/*.svg xxx/customicon/*.eot xxx/customicon/*.woff xxx/customicon/*.ttf
)
app.config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
end
end
end
The rails engine getting started guide suggests including the following in your engine.rb
initializer "engine_name.assets.precompile" do |app|
app.config.assets.precompile += %w( admin.js admin.css )
end
It works for me.
I faced same issue with Rails 4 engine, I resolved by adding following code to engine.rb
Rails.application.config.assets.precompile += ['*.js', '*.css', '**/*.js', '**/*.css', '*.jpg',
'*.png', '*.ico', '*.gif', '*.woff2', '*.eot',
'*.woff', '*.ttf', '*.svg']
I need to precompile all assets so wild cards are used. You can specify files.
Second option is you can specify on Host applications
config/assets.rb
Rails.application.config.assets.precompile += %w( engine_name/file_name )
EDIT Just realised this question is a bit old. The below will work for you if you add an app/assets/config/my_component_manifest.js file to your engine and then use that to specify asset entry points.
You an also put it in the engine.rb file:
module MyComponent
class Engine < ::Rails::Engine
config.assets.precompile += %w( my_component_manifest.js )
end
end

Rails 3.2.13 - Assets are not displayed in production

I've spent the whole day trying to figure out this issue, but without any positive results.
When I load my website, there are no CSS, images or working javascript.
The app is running on Rails 3.2.13 and Capistrano 2.
Here's my setup:
config/environments/production.rb
Appname::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.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.precompile = ['*.js', 'application.css', 'styles.css.scss', '*.css.erb']
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.svg *.woff *.ttf *.ico)
config.assets.digest = true
config.cache_store = :memory_store
config.i18n.fallbacks = true
config.active_support.deprecation = :notify
end
application.rb
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
Bundler.require(*Rails.groups(:assets => %w(development test)))
end
module Apname
class Application < Rails::Application
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
config.assets.enabled = true
config.assets.paths << "#{Rails.root}/app/assets/fonts"
config.assets.precompile << /\.(?:svg|eot|woff|ttf)\z/
config.assets.version = '1.0'
config.assets.initialize_on_precompile = false
end
end
I ran locally bundle exec rake assets:precompile -> the files were generated to the public/assets directory.
But still -- there are no assets on production.
I have already no idea what to change in the config files, still the same result on the production server.
Every help will be warmly appreciated!
EDIT:
I just noticed in the console that images are loaded correctly (I can load http://website.com/assets/social_icons/facebook_ico.png), but not CSS+JS (http://IP/assets/application-3b6ba7703e465259e4e7ebee17c5ea1e.js 404 NOT FOUND - the same for .css)
Did you try to restart the webserver after compiling the assets?
Are the rights set correctly (chmod 755 . -R)?
Also, check that your webserver configuration points to app_root/public and not just app_root
EDIT1:
Check if you can load the url directly. In addition verify that the rendered page display correctly-cached urls.
If one of the two fails try forcing compilation for production environment
rake assets:precompile RAILS_ENV=production
EDIT2:
If nothing works try rolling back to default assets config for production:
# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false
# Compress JavaScripts and CSS.
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
# Do not fallback to assets pipeline if a precompiled asset is missed.
config.assets.compile = true
# Generate digests for assets URLs.
config.assets.digest = true
# Version of your assets, change this if you want to expire all your assets.
config.assets.version = '1.0'
instead of:
#config.serve_static_assets = false
config.serve_static_assets = true
config.assets.compress = true
config.assets.compile = true
config.assets.precompile = ['*.js', 'application.css', 'styles.css.scss', '*.css.erb']
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif *.svg *.woff *.ttf *.ico)
config.assets.digest = true
EDIT 3:
EDIT:
I just noticed in the console that images are loaded correctly (I can
load http://website.com/assets/social_icons/facebook_ico.png), but not
CSS+JS
(http://****/assets/application-3b6ba7703e465259e4e7ebee17c5ea1e.js 404
NOT FOUND - the same for .css)
That also means you aren't actually compiling assets. If you were, you'd need to load facebook_ico.png with the hash attached to the name

Resources