在Rails 5应用程序中,我们尝试加载自定义字体。在开发中,这可以按预期工作,但在将资产前缀设置为/static
或static
的分段中,它会在请求/static/static/font_name
时生成404字体。
我们正在使用Webpack编译资产,我们尝试删除前缀并将所有内容编译为公共资产,但此时没有任何项正确加载。如果我们检查服务器,则字体已被指纹识别,并且位于应用程序的static
目录中。
字体的SCSS文件
@font-face {
font-family: 'neuzeits_book';
src: font-url('neuzeits_book.eot');
src: font-url('neuzeits_book.eot#iefix') format('embedded-opentype'),
font-url('neuzeits_book.woff2') format('woff2'),
font-url('neuzeits_book.woff') format('woff'),
font-url('neuzeits_book.svg') format('svg'),
font-url('neuzeits_book.ttf') format('truetype');
}
$font_body: 'neuzeits_book', arial;
以下是暂存时生成的css。
@font-face {
font-family: 'neuzeits_book';
src: url(static/neuzeits_book-feb4879f32fd707ba88e9d1f9f2a5e901042ab66a3e01560d9b73f22ded85185.eot);
src: url(static/neuzeits_book-feb4879f32fd707ba88e9d1f9f2a5e901042ab66a3e01560d9b73f22ded85185.eot#iefix) format("embedded-opentype"),url(static/neuzeits_book-874e588d20dc28011a445eb8f645f610b6a312a7217bab344d1394187d96334a.woff2) format("woff2"),url(static/neuzeits_book-1228efc2d44dc6f54667fcba56588d3ad66c689b73e64f382370c23ebaa00975.woff) format("woff"),url(static/neuzeits_book-903eaa19a9dc9ae8210827ebf4591347271825b13bb4d25efb1fdacb1f8b9dab.svg) format("svg"),url(static/neuzeits_book-a34d20b4bf82e78b20547e4d32a4846acefc2e1f96e30ed9186a1b1594963d32.ttf) format("truetype")
}
以下是暂存资产的配置。
config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
config.assets.js_compressor = :uglifier
# config.assets.css_compressor = :sass
config.react.variant = :production
config.assets.compress = true
config.assets.compile = false
config.assets.digest = true
config.assets.enabled = true
config.assets.initialize_on_precompile = true
config.assets.prefix = '/static'
config.assets.paths << Rails.root.join('node_modules')
config.assets.paths << Rails.root.join('app', 'assets', 'fonts')