相反,我创建了/ public / fonts,将所有字体放在那里并用url(‘/ fonts / myAwesomeFont.ttf’)引用它们.一切都很完美.
这种方法有不利之处吗?
字体文件是否在资产预编译中压缩?
我们将字体保存在/ app / assets / fonts文件夹中,对于我的记忆,我们不需要进行任何其他配置.但是,在引用字体时,您必须在CSS文件中使用asset_path帮助程序(与在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
@ 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; }
使用资产管道进行二进制文件的好处
我们不对图像或字体资产进行任何类型的预编译(我猜你可以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"
如果您已经使用asset_path助手引用了您的字体,那么这些URL将自动更新以指向CDN.我想使用path_helpers作为资产管道本身也是一个好处,但无论哪种方式都是有益的.
希望这可以帮助!
精彩评论