A user reported an issue with code highlighting not working on their site due to a CORS policy block. pfaffman replicated the issue and found that the problem was caused by the lack of an ‘Access-Control-Allow-Origin’ header on the requested resource from the CDN.
pfaffman fixed the issue by adding js to the “add cors header” setting on the CORS page, which allowed bunny.net to automatically add CORS headers to all requests to files with the js extension.
itbhanusharma mentioned that there might be a setting in bunny.net that adds js to CORS, but pfaffman was unsure if this was a recent change.
david explained that the issue was likely caused by a recent change in Discourse that switched highlightjs to load via native import(), which requires CORS headers when fetching from a CDN. He also mentioned that Discourse should be adding the CORS header to CDN responses, but if it’s not, adding the headers via the CDN config can achieve the same result.
Someone compained that code highlighting isn’t working on their site. I’ve replicated the issue on mine. The issue seems to be that the CDN is blocked by CORS policy:
I fixed the problem by adding js to the “add cors header” setting on the cors page:
I believe there is (or used to be) a setting in bunny which added js to cors? I (for some reason) have js added to cors for all my bunny.net resources but I don’t recall adding them myself. Is it a fairly new site?
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, that probably means your CDN is not setting the CDN hostname (i.e. not the forum hostname) in the Host header or the X-Forwarded-Host header.
That said, adding the headers via the CDN config likely achieves the same thing