Rails 7带有JavaScript软件包的进口地图来自CDN投掷CORS错误
我正在尝试使用新的Rails 7 Import Maps功能导入在CDN上托管的JS软件包。但是,每当我的Rails项目加载时,我都会在此形式中获得CORS
相关错误:
Access to script at 'https://cdn.vender-application.com/package.js' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
我的importmap.rb
看起来像这样:
pin "va_cdn", to: "https://cdn.vender-application.com/package.js"
我添加了rack-cors < /code> gem以及
cors.rb
文件,但是没有效果。示例CORS文件:
- 我首先尝试使用常规
config/initializers/cors.rb
:
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: :any
end
end
- 失败后,我尝试变得更具体,因为我听说某些服务不喜欢
*<< /代码>原点。我使用
http://127.0.0.1:3000
这是错误消息中列出的确切来源。我尝试使用资源'*'
和一个更具体的尝试
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'http://127.0.0.1:3000'
resource '/', headers: :any, methods: :any
resource '/index', headers: :any, methods: :any
end
end
。 此代码将成功获得包:
<% content_for :head do %>
<%= javascript_include_tag "https://cdn.vender-application.com/package.js" %>
<% end %>
我更喜欢使用新的推荐方法获取此软件包,而不是插入呼叫。
I'm trying to import a JS package hosted on a CDN using the new Rails 7 import maps feature. However, whenever my Rails project loads I get a CORS
related error in this form:
Access to script at 'https://cdn.vender-application.com/package.js' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
My importmap.rb
looks something like this:
pin "va_cdn", to: "https://cdn.vender-application.com/package.js"
I added the rack-cors
gem along with a cors.rb
file, however it had no effect. Example cors files:
- I first tried with a general
config/initializers/cors.rb
:
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', headers: :any, methods: :any
end
end
- After that failed I tried being more specific as I've heard some services don't like an
*
Origin. I usedhttp://127.0.0.1:3000
as that was the exact origin listed in the error message. I tried both withresource '*'
and a more specific one:
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins 'http://127.0.0.1:3000'
resource '/', headers: :any, methods: :any
resource '/index', headers: :any, methods: :any
end
end
I seems like it something specific to importmaps, since
this code will successfully get the package:
<% content_for :head do %>
<%= javascript_include_tag "https://cdn.vender-application.com/package.js" %>
<% end %>
I'd prefer to use the new recommended way of getting this package, rather than inlining the call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否为软件包运行
bin/importMap PIN
?ImportMaps将在本地获得供应商,并致力于您的回购。看来您正在尝试从CDN实时加载它,而我从未见过
import map-rails
使用这种方式。https://github.com/rails/rails/importmap-rails#usage
这就是什么我的
importmap.rb
看起来像。Did you run
bin/importmap pin
for your package?Importmaps get vendored locally and committed to your repo. It looks like you're trying to load it live from the CDN and I've never seen
import map-rails
used this way.https://github.com/rails/importmap-rails#usage
And here's what my
importmap.rb
looks like.