Ruby on rails Rails字体CORS策略
我无法为CORS策略加载此字体 文件夹:Ruby on rails Rails字体CORS策略,ruby-on-rails,cors,ruby-on-rails-5,Ruby On Rails,Cors,Ruby On Rails 5,我无法为CORS策略加载此字体 文件夹:app/assets/font/Inter UI.var.woff2 <%=preload_link_tag("Inter-UI.var.woff2", as:'font', crossorigin: "anonymous")%> 应用程序.rb Rails.application.configure do config.middleware.insert_before 0, Rack::Cors do allow do
app/assets/font/Inter UI.var.woff2
<%=preload_link_tag("Inter-UI.var.woff2", as:'font', crossorigin: "anonymous")%>
应用程序.rb
Rails.application.configure do
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => :any
end
end
config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2)$/
Rails.application.config.assets.paths << Rails.root.join("app", "assets", "fonts")
尝试将此添加到
application.rb
Rails.application.config.assets.precompile << /\.(?:svg|eot|woff|ttf|woff2)$/
与
你可以简单地使用
<%= preload_link_tag("Inter-UI.var.woff2") %>
@font-face {
font-family: 'Inter UI';
font-style: italic;
font-weight: 400;
font-display: swap;
unicode-range: U+000-5FF;
src: font_url("Inter-UI.var.woff2") format("woff2-variations"),
font_url("Inter-UI-Italic.woff2") format("woff2"),
font_url("Inter-UI-Italic.woff") format("woff");
}
还有do
rake资产:预编译
响应的HTTP状态代码是什么?您可以使用浏览器devtools中的网络窗格进行检查。是4xx还是5xx错误,而不是200 OK成功响应?如果粘贴http://localhost:3000/assets/Inter-UI.var-e2e323d19d24946c4d481135af27ba00f3266aa9d4abe4262e97088feccb6ca4.woff2
插入浏览器地址栏,并尝试直接在浏览器中打开它?它只会显示“(失败)net::ERR_failed”(添加屏幕截图)。如果我直接转到字体文件URL,它会成功下载。您是否使用0.0.0.0
以任何形式在浏览器中打开站点或启动Rails服务器?我不知道我是否理解了您的意思。我使用“rails s”启动服务器,它给我地址0.0.0.0:3000。每个url都是由您运行的railscan生成的rails服务器--绑定本地主机--端口3000
?谢谢。它已经存在了。即使结尾有“woff2”,好吧,我再也不会有错误了。但没有被CSSDone加载,但没有任何更改。没有显示错误,字体不显示。我不知道。这是因为字体位置不正确。你会用新的测试吗
Rails.application.configure do
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => :any
end
end
end
Rails.application.configure do
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => :any
end
end
allow do
origins "*"
resource "/assets/*", methods: :get, headers: :any
end
end
<%= preload_link_tag("Inter-UI.var.woff2") %>
@font-face {
font-family: 'Inter UI';
font-style: italic;
font-weight: 400;
font-display: swap;
unicode-range: U+000-5FF;
src: font_url("Inter-UI.var.woff2") format("woff2-variations"),
font_url("Inter-UI-Italic.woff2") format("woff2"),
font_url("Inter-UI-Italic.woff") format("woff");
}
@font-face {
font-family: 'Inter UI';
font-style: italic;
font-weight: 400;
font-display: swap;
unicode-range: U+000-5FF;
src: url(<%= asset_path "Inter-UI.var.woff2" %>) format("woff2-variations"),
url(<%= asset_path "Inter-UI-Italic.woff2" %>) format("woff2"),
url(<%= asset_path "Inter-UI-Italic.woff" %>) format("woff");
}