SVG Fonts with Rails Asset Pipeline and S3 Hosting

2019-08-08 07:54发布

问题:

I have a Rails project, hosted on Heroku, and I just moved hosting my assets to S3. This has all gone smooth EXCEPT for some custom fonts (SVG fonts from icomoon). They're not working, and when I view my web source, I can see that my S3 bucket doesn't show up:

@font-face{font-family:'Starter-Icons';src:url(https://.s3.amazonaws.com/fonts/Starter-Icons.eot);src:url(https://.s3.amazonaws.com/fonts/Starter-Icons.eot?#iefix) 

etc. However, other assets (images, stylesheets, etc) hosted with S3 do include the proper bucket name.

My font-face declarations are in a .less file (this doesn't need to be a .less.erb file does it?)

@font-face {
font-family: 'Starter-Icons';
src:font-url("Starter-Icons.eot");
src:font-url("Starter-Icons.eot?#iefix") format('embedded-opentype'),
    font-url("Starter-Icons.woff") format('woff'),
    font-url("Starter-Icons.ttf") format('truetype'),
    font-url("Starter-Icons.svg#Starter-Icons") format('svg');
font-weight: normal;
font-style: normal;

}

All my fonts are in /app/assets/fonts. I've followed the steps in the accepted answer here, but the fact that my bucket name isn't showing up for the fonts leads me to believe it's a different issue.

EDIT

I am using the asset_sync gem for this, its config is below:

if defined?(AssetSync)
  AssetSync.configure do |config|
  config.fog_provider = 'AWS'
  config.fog_directory = 'starterapp'

  config.aws_access_key_id = ENV['STARTER_AWS_ACCESS_KEY_ID']
  config.aws_secret_access_key = ENV['STARTER_AWS_SECRET_ACCESS_KEY']

  # Don't delete files from the store
  config.existing_remote_files = 'keep'

  # Automatically replace files with their equivalent gzip compressed version
  config.gzip_compression = true
end

end

回答1:

Well, (un)fortunately -- it works now, and all I did is bundle exec rake assets:precompile (again) and push to Heroku (again). The bundle exec rake assets:precompile cleared out the old compiled css/js/font files, and built new ones. I did this step previously and didn't get these results, so I'm not sure how it's even working.

This could've been user error--I don't see how this actually made it work. Another confusing thing is running

irb(main):001:0> Rails.application.config.assets.paths

returns

=> ["/app/app/assets/fonts", "/app/app/assets/images", "/app/app/assets/javascripts", "/app/app/assets/stylesheets"

and I'm not sure how /app/app/assets is getting created on production vs /app/assets/ on development, but, it works.