# 相对路径导致的CORS错误在Discourse网站上

**URL:** https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024
**Category:** Bug
**Created:** [2024年十月27日 06:27 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024 "2024-10-27T06:27:11Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Yt.w](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yt.w/32/348367_2.png) [@Yt.w](https://meta.discourse.org/u/Yt.w)
#### Post date: [2024年十月27日 06:27 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024/1 "2024-10-27T06:27:11Z")

</div>

**lib/highlight\_js.rb** 中的以下代码在某些域上有效，但在其他域上无效：

```ruby
def self.path
    "/highlight-js/#{Discourse.current_hostname}/#{version SiteSetting.highlighted_languages}.js"
end

```

我有两个 Discourse 站点。此代码在域 `www.abc.com` 上有效，但在 `efg.com` 上无效。在浏览器控制台中，我看到以下错误：

`formatter.js:383 Uncaught (in promise) TypeError: Failed to resolve module specifier '/highlight-js/efg.com/9797975efac87d28baa695ae13ca72ccaf5120f5.js'. `The base URL is about:blank because import() is called from a CORS-cross-origin script.

在对 highlight\_js.rb 中的 self.path 进行如下修改后：

```ruby
def self.path
    "https://#{Discourse.current_hostname}/highlight-js/#{Discourse.current_hostname}/#{version SiteSetting.highlighted_languages}.js"
end

```

该问题已解决。

看起来使用相对路径，如  
`import('/highlight-js/efg.com/9797975efac87d28baa695ae13ca72ccaf5120f5.js')`  
会导致 CORS 问题，而使用绝对 URL，  
`import('https://efg.com/highlight-js/efg.com/9797975efac87d28baa695ae13ca72ccaf5120f5.js')`  
则可以正常工作。

---

<div class="post-metadata">

### Author: ![NateDhaliwal](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/natedhaliwal/32/313494_2.png) [@NateDhaliwal](https://meta.discourse.org/u/NateDhaliwal)
#### Post date: [2024年十月27日 08:22 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024/2 "2024-10-27T08:22:17Z")

</div>

奇怪的是，它在 `abc.com` 下可以工作，但在 `efg.com` 下卻不行，因為這兩個不同的路由是完全不同的 🤔

---

<div class="post-metadata">

### Author: ![Yt.w](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yt.w/32/348367_2.png) [@Yt.w](https://meta.discourse.org/u/Yt.w)
#### Post date: [2024年十月27日 09:04 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024/3 "2024-10-27T09:04:35Z")

</div>

我的意思是它适用于 `www.abc.com` 但不适用于 `efg.com`，不确定为什么，但它发生在我的案例中

---

<div class="post-metadata">

### Author: ![simonk](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simonk/32/247950_2.png) [@simonk](https://meta.discourse.org/u/simonk)
#### Post date: [2024年十月28日 11:49 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024/4 "2024-10-28T11:49:35Z")

</div>

您是否在这两个网站上使用了 CDN？我注意到我的网站上的代码高亮已不再有效，我认为这是因为：

> [@Code highlighting failed because bunny.net CDN](https://meta.discourse.org/t/code-highlighting-failed-because-bunny-net-cdn/296333/4?u=simonk):
>
> The change which made this noticeable was most likely [DEV: Modernise highlightjs loading (#24197) · discourse/discourse@0878dde · GitHub](https://github.com/discourse/discourse/commit/0878dde2132a34ba2787e702de222bc6dd161895), which switched highlightjs to load via native import(), which requires CORS headers when fetching from a CDN. But even before that change, you likely would have had some issues with things like custom Fonts (which also require CORS headers for cross-origin requests). In theory, Discourse itself should be adding the CORS header to CDN responses. If it’s not, …

就我而言，我的 CDN **没有** 为 highlightjs 文件返回 `Access-Control-Allow-Origin` 标头。我注意到 Meta 的 CDN **确实** 包含该标头，所以我想知道有什么不同。

```plaintext
$ curl --silent -I https://d3bpeqsaub0i6y.cloudfront.net/highlight-js/meta.discourse.org/9797975efac87d28baa695ae13ca72ccaf5120f5.js | grep -i access-control
access-control-allow-origin: *
access-control-allow-methods: GET, HEAD, OPTIONS

```

但是，这些标头 **没有** 由源服务器提供：

```plaintext
$ curl --silent -I https://meta.discourse.org/highlight-js/meta.discourse.org/9797975efac87d28baa695ae13ca72ccaf5120f5.js | grep -i access-control

```

据我所知，Discourse 应该为 highlightjs 文件添加访问控制标头：

> <https://github.com/discourse/discourse/blob/27c20eeacd0ac2ae8c69cfb0753abda581357306/app/controllers/highlight_js_controller.rb#L11>

**但是** ，只有当请求是“CDN 请求”时，这些标头才会被应用：

> <https://github.com/discourse/discourse/blob/27c20eeacd0ac2ae8c69cfb0753abda581357306/app/controllers/application_controller.rb#L789>

> <https://github.com/discourse/discourse/blob/27c20eeacd0ac2ae8c69cfb0753abda581357306/lib/discourse.rb#L1187>

这仅在 Discourse 配置了单独的“CDN 请求”主机名时才有效。

---

<div class="post-metadata">

### Author: ![simonk](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simonk/32/247950_2.png) [@simonk](https://meta.discourse.org/u/simonk)
#### Post date: [2024年十月28日 12:02 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024/5 "2024-10-28T12:02:16Z")

</div>

有一个设置 `cdn_origin_hostname`，或许可以设置为普通的 discourse 主机名：

> <https://github.com/discourse/discourse/blob/27c20eeacd0ac2ae8c69cfb0753abda581357306/app/models/global_setting.rb#L125>

> <https://github.com/discourse/discourse/blob/27c20eeacd0ac2ae8c69cfb0753abda581357306/config/discourse_defaults.conf#L129>

我认为这会使 `is_cdn_request` 检查通过，但我不知道是否会有任何负面影响。

---

<div class="post-metadata">

### Author: ![Yt.w](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/yt.w/32/348367_2.png) [@Yt.w](https://meta.discourse.org/u/Yt.w)
#### Post date: [2024年十月28日 12:48 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024/6 "2024-10-28T12:48:13Z")

</div>

> [@simonk](#):
>
> 您是否在这两个网站上使用了 CDN？

是的，我在 `efg.com` 上使用 CDN，但当我访问 js 文件时，它确实返回了我正确的 **Access-Control-Allow-Origin** ，这让我很困惑。

---

<div class="post-metadata">

### Author: ![simonk](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simonk/32/247950_2.png) [@simonk](https://meta.discourse.org/u/simonk)
#### Post date: [2024年十月29日 17:48 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024/7 "2024-10-29T17:48:29Z")

</div>

> [@simonk](#):
>
> 有一个设置 `cdn_origin_hostname`，也许可以设置为普通的 discourse 主机名

作为后续，我将 `cdn_origin_hostname` 设置为我的 Discourse 实例的常规域名，刷新了 CDN 缓存，现在 highlightjs 又可以正常工作了。我还没有_注意到_任何副作用……

---

<div class="post-metadata">

### Author: ![cuo\_wu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/cuo_wu/32/498442_2.png) [@cuo\_wu](https://meta.discourse.org/u/cuo_wu)
#### Post date: [2025年五月18日 11:34 UTC](https://meta.discourse.org/t/issue-with-relative-path-causing-cors-error-on-discourse-sites/333024/8 "2025-05-18T11:34:16Z")

</div>

我也遇到这个问题。有什么更新吗？
