RSG*_*RSG 5 fonts ruby-on-rails ruby-on-rails-3.1 asset-pipeline
我喜欢资产管道,但对于我的生活,我无法让它服务我的字体.我浪费了几个小时谷歌搜索和尝试SO解决方案(nope,nope,nope).
相反,我创建了/public/fonts,删除了我的所有字体并引用它们url('/fonts/myAwesomeFont.ttf').一切都很完美.
这种方法有不利之处吗?
字体文件是否在资产预编译中压缩?
我们将字体保存在/ app/assets/fonts文件夹中,对于我的记忆,我们不需要进行任何其他配置.
但是,asset_path在引用字体时,您必须在CSS文件中使用帮助程序(与在CSS中引用资产管道图像时相同).这需要将文件扩展名从.css更改为.css.erb.从你的描述中很难确定,但我猜这可能是问题.
我们在应用程序中使用Museo500字体并将其存储在app/assets/fonts中:
app/assets/fonts:
- museo700-regular-webfont.eot
- museo700-regular-webfont.woff
- museo700-regular-webfont.ttf
- museo700-regular-webfont.svg
Run Code Online (Sandbox Code Playgroud)
@ font-face声明如下:
@font-face {
font-family: 'Museo700';
src: url('<%= asset_path "museo700-regular-webfont.eot" %>');
src: url('<%= asset_path "museo700-regular-webfont.eot" %>?#iefix') format('embedded-opentype'),
url('<%= asset_path "museo700-regular-webfont.woff" %>') format('woff'),
url('<%= asset_path "museo700-regular-webfont.ttf" %>') format('truetype'),
url('<%= asset_path "museo700-regular-webfont.svg" %>#Museo700') format('svg');
font-weight: normal;
font-style: normal;
}
Run Code Online (Sandbox Code Playgroud)
我们不对图像或字体资产进行任何类型的预编译(我猜你可以gzip字体或其他东西,但我们没有)但我仍然看到通过资产管道托管它们的好处:统一性和约定.使用Rails约定可以提供各种好处.
例如:在某些时候,您可能希望使用像Amazon Cloudfront这样的CDN,并且需要将生产中的所有资产URL指向CDN副本.如果您通过资产管道托管所有资产(包括字体和图像),则通过取消注释行更改就可以轻松更改production.rb文件中的asset_host:
# Enable serving of images, stylesheets, and JavaScripts from an asset server
# config.action_controller.asset_host = "http://assets.example.com"
Run Code Online (Sandbox Code Playgroud)
如果您已经使用asset_path帮助程序引用了字体,则这些URL将自动更新以指向CDN.我想使用path_helpers作为资产管道本身也是一个好处,但无论哪种方式都是有益的.
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
2773 次 |
| 最近记录: |