Embedding comments widget on localhost causes FrozenError

AI-generated summary

Dev_Singh is trying to embed Discourse comments on a Rails app running at localhost:5000, with Discourse itself on localhost:3000. The embed widget shows “Loading Discussion…” but fails silently. Tracing reveals a FrozenError in TopicEmbed.absolutize_url when attempting to append port 5000 to a frozen string. Despite experimenting with removing # frozen_string_literal: true, the error persists. codinghorror clarifies that Discourse does not support running on non-standard ports, implying this is a known limitation rather than a bug. The issue stems from Discourse’s inability to handle embedded URLs with custom ports, and no workaround is suggested beyond using standard ports (80/443).

I have a rails site running on localhost:5000 and a local Discourse server is running on localhost:3000. I want to enable comments on articles (which is a rails resource in my case), so I’ve set up an embeddable host in the Discourse admin panel:

This is the script snippet I’ve added on my rails view:

<div id='discourse-comments'></div>

<script type="text/javascript">
  DiscourseEmbed = { discourseUrl: 'http://localhost:3000/',
                 discourseEmbedUrl: '<%= "http://localhost:5000/articles/#{@article.id}" %>'};

  (function() {
   var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;
   d.src = DiscourseEmbed.discourseUrl + 'javascripts/embed.js';
   (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body') 
  [0]).appendChild(d);
 })();

I’m getting the “Loading Discussion…” message but nothing further. I’ve tried tracing the issue and this is what I think is happening.

On Sidekiq the job Jobs::RetrieveTopic fails with Jobs::HandledExceptionWrapper: Wrapped FrozenError: can't modify frozen String.

I’ve traced it back in the code to line 192 in TopicEmbed.absolutize_url method:

prefix = "#{uri.scheme}://#{uri.host}"
prefix << ":#{uri.port}" if uri.port != 80 && uri.port != 443

Since my port is 5000 (not 80, or 443) the code attempts to concatenate the port to the prefix string and a FrozenError is raised. The RetrieveTopic job has the pragma comment # frozen_string_literal: true. I’ve actually tried removing it as an experiment but the problem persists, so I’m not sure if all string literals are frozen by default in Discourse.

Any tips on how to make this work locally? Also, if string literals are indeed supposed to be frozen, is this a bug?

Discourse does not support running on non-standard ports.