6

in my rails 4 project css uses fonts files. so they need to be precompiled additionally.

i achieve that with adding following lines to config/environments/production.rb

  # Add the fonts path
  config.assets.paths << Rails.root.join('vendor', 'assets', 'fonts','fonts')
  #
  # # Precompile additional assets
  config.assets.precompile += %w( *.svg *.eot *.woff *.ttf )

and running rake assets:precompile in production.

however, result is following:

I, [2013-10-10T19:27:51.931963 #16052]  INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-ab2f6984951c07fd89e6afdefabd93c7.eot
I, [2013-10-10T19:27:51.940615 #16052]  INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-24dfb40c91db789b8b8faba6886ac1ef.svg
I, [2013-10-10T19:27:51.950685 #16052]  INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-4b2130768da98222338d1519f9179528.ttf
I, [2013-10-10T19:27:51.983230 #16052]  INFO -- : Writing /var/lib/openshift/521e19c85004460a8e000107/app-root/runtime/repo/public/assets/fonts/glyphicons-halflings-regular-7a07f26f72466361ac9671de2d33fd1c.woff

and css files refer to font files without this md5 fingerprint.

how can i precompile assets so that they get generated without md5 fingerprint? or should i just put them to public/fonts/ folder in such case?

Pavel K.
  • 6,697
  • 8
  • 49
  • 80

1 Answers1

2

Follow these steps

  • Your font must be in app/assets/fonts
  • Add your font to the assets path (like you did) but prefer config/application.rb
  • Declare your font in your CSS with à @font-face. You'll find some help here
  • If you don't use SCSS you must have embedded css like application.css.erb and use asset_path() helper to implement your fonts path in your font-face declaration.

An exemple without SCSS :

@font-face {
  font-family: 'MyFont';
  src:url('<%= asset_path("myfont.eot")%>');
  src:url('<%= asset_path("myfont.eot?#iefix")%>') format('embedded-opentype'),
    url('<%= asset_path("myfont.svg#myfont")%>') format('svg'),
    url('<%= asset_path("myfont.woff")%>') format('woff'),
    url('<%= asset_path("myfont.ttf")%>') format('truetype');
  font-weight: normal;
  font-style: normal;
}
Ludovic
  • 1,992
  • 1
  • 21
  • 44
  • 2
    the problem not in how to create fonts the problem in when you precompile assets for production font creates but with md5 fingerprint. But after in Network console have fontawesome-webfont-62877.woff 404 error. It looks like rails do not see precompile assets in publick/assets fontawesome-webfont-62877-e70f92449ebfddada3d455eb44542655.woff but when i am add file fontawesome-webfont-62877.woff without md5 it is work fine. How to create precompile that rails see fonts in production. – Sergey Chechaev Nov 17 '13 at 12:39
  • @SergeyChechaev With my method everything work in production mode. With rails3 I've got issues, the solution was to copy font directory in public/assets/. If you don't want to precompile the fonts why do you want to add them into assets system ? It's a bit weird :) – Ludovic Nov 17 '13 at 13:04
  • i don't understand how to precompile assets in production mode that it is fork fine i have 404 error. – Sergey Chechaev Nov 17 '13 at 13:24
  • @SergeyChechaev read the answer closely - you need to change your (S)CSS files to have a .erb extension so that on precompilation, the fingerprinted font file path is included instead of the raw filename. – Mike Atlas Dec 20 '13 at 18:03