Custom Font not working in Rails Asset Pipeline

别来无恙 提交于 2019-12-24 17:43:29

问题


I have this file structure in my Rails app:

\APP
+---assets
|   +---fonts
|   |       icons.eot
|   |       icons.svg
|   |       icons.ttf
|   |       icons.woff
|   +---icons
|   +---images
|   +---javascripts
|   +---stylesheets
+---controllers
|
... etc ...

The custom font I have (icons) works in development, but not in production (or staging). I know it has something to do with the asset pipeline (404 errors). How do I get my icons / font to show up?

My _icons.scss file (included in application.css.scss):

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

@media screen and (-webkit-min-device-pixel-ratio: 0) {
  @font-face {
    font-family: "icons";
    src: font-url("icons.svg#icons") format("svg");
  }
}

[data-icon]:before {
  content: attr(data-icon);
}

[data-icon]:before,
.icon-ico-addcart:before,
.icon-ico-addcart-hover:before,
.icon-ico-allparsers-menu:before,
.icon-ico-blog-menu:before,
.icon-ico-cart:before,
...

The errors I get:

GET http://127.0.0.1:8888/assets/icons.woff 
GET http://127.0.0.1:8888/assets/icons.ttf 404 (Not Found)      application-e60c1d16b23dd8ae118e0bb3a1d3311c.js:6129 

I am precompiling before. My production environment is my own server. The font was generated using fontcustom with svgs. I used this article to do so.

The font is being precompiled... If I edit the application.css file on the page, changing

url('/assets/icons.woff')

to

url('/assets/icons-8695a8c5b193c6423e0b3b7a9c71b808.woff')

my images appear. This means that my assets are precompiling, but I'm not able to reference them correctly in my scss file.


回答1:


What are you using for production? Heroku?

Try precompiling those types of files:

 # In config/initializers/assets.rb
 Rails.application.config.assets.precompile += %w( '.ttf' ) 

Include all the font endings. You could also try including the font by CDN instead.

If that doesn't work, maybe check out this answer: Rails 4: Why are fonts not loading in production?




回答2:


It could be that the content type (application/x-font-woff) isn't served along with the asset, I'd start there.



来源:https://stackoverflow.com/questions/32334157/custom-font-not-working-in-rails-asset-pipeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!