Rails 7带有JavaScript软件包的进口地图来自CDN投掷CORS错误

发布于 2025-02-09 21:56:19 字数 1454 浏览 3 评论 0原文

我正在尝试使用新的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 used http://127.0.0.1:3000 as that was the exact origin listed in the error message. I tried both with resource '*' 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月流歌 2025-02-16 21:56:19

您是否为软件包运行bin/importMap PIN

ImportMaps将在本地获得供应商,并致力于您的回购。看来您正在尝试从CDN实时加载它,而我从未见过import map-rails使用这种方式。

https://github.com/rails/rails/importmap-rails#usage

这就是什么我的importmap.rb看起来像。

pin "application", preload: true

pin "@hotwired/turbo-rails", to: "turbo.js"
pin "@hotwired/stimulus", to: "stimulus.min.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin "form-request-submit-polyfill" # @2.0.0
pin "@vimeo/player", to: "@vimeo--player.js" # @2.18.0
pin "js-cookie" # @3.0.1
pin "jcrop" # @3.0.1
pin "hammerjs" # @2.0.8
pin "@rails/request.js", to: "@rails--request.js.js" # @0.0.9

pin_all_from "app/javascript/lib", under: "lib"
pin_all_from "app/javascript/controllers", under: "controllers"
pin_all_from "app/javascript/helpers", under: "helpers"
pin_all_from "app/components", under: "components"

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.

pin "application", preload: true

pin "@hotwired/turbo-rails", to: "turbo.js"
pin "@hotwired/stimulus", to: "stimulus.min.js"
pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
pin "form-request-submit-polyfill" # @2.0.0
pin "@vimeo/player", to: "@vimeo--player.js" # @2.18.0
pin "js-cookie" # @3.0.1
pin "jcrop" # @3.0.1
pin "hammerjs" # @2.0.8
pin "@rails/request.js", to: "@rails--request.js.js" # @0.0.9

pin_all_from "app/javascript/lib", under: "lib"
pin_all_from "app/javascript/controllers", under: "controllers"
pin_all_from "app/javascript/helpers", under: "helpers"
pin_all_from "app/components", under: "components"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文