Hello, I’m trying to embed Discourse comments on my website, following the embedding guide, and I’ve run into a wall
Symptoms
I’ve tried in Firefox and Chrome. In both cases, it loads the “Loading Discussion…” discourse iframe but hangs there, with recurring JavaScript errors in the developer console.
On Firefox, I get an error about X-Frame-Options:
Invalid X-Frame-Options header was found when loading “https://discourse.29th.local/embed/comments?embed_url=https%3A%2F%2Fpersonnel.29th.local%2F%23enlistments%2F11927”: “ALLOWALL” is not a valid directive.
Followed by a DOMException error in embed-application.js:7
:
Uncaught DOMException: An invalid or illegal string was specified
These two errors repeat every 30 seconds or so. There are no failed requests in the network tab.
In Chrome, I don’t get the X-Frame-Options error. After a few seconds, I get an error about the target origin not matching the recipient window’s origin:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('https://discourse.29th.local') does not match the recipient window's origin ('https://personnel.29th.local').
I’ve seen plenty of topics on meta about this error, and I’ve tried all the troubleshooting steps, to no avail.
My setup
I followed the discourse for mac setup guide with a slight exception: instead of installing postgres, redis, and mailcatcher globally on my laptop, I have them running in docker containers, with the ports publicly exposed. Discourse has no idea they’re running in docker containers instead of bare metal. Rails/discourse is installed globally, and not running in a docker container.
Entirely separately, my custom web application is running in a docker compose stack. Part of that stack includes an nginx server which routes personnel.29th.local
to the appropriate upstream container, and discourse.29th.local
to host.docker.internal:3000
(that’s the magic hostname that docker containers can use to reach the host’s localhost
).
(As I mention below, I’ve removed the nginx layer from the equation and ended up with the same error)
One possible gotcha here is that my web app is a JavaScript single page app. The page where discourse comments are being embedded is https://personnel.29th.local/#enlistments/1234
and there is no server-side rendering. If that were a problem, I would expect an error with the crawler, at which point I’d settle for Discourse simply linking to my app, rather than crawling it. But the errors it shows don’t appear to relate to crawling failures.
Troubleshooting
I’ve set the embeddable host in Admin > Customize > Embedding to personnel.29th.local
. At first, the sample embed code showed http://localhost:3000/
for the discourseUrl
, so I fired up rails console
and ran:
SiteSetting.force_hostname = "discourse.29th.local"
SiteSetting.port = 443
And turned on “force https” in the admin dashboard. This fixed the url in the sample embed code.
I also added https://personnel.29th.local
as a CORS domain in the settings’ cors origins
section.
I’m now starting discourse with the following command:
DISCOURSE_DEV_HOSTS=discourse.29th.local,host.docker.internal DISCOURSE_ENABLE_CORS=true bundle exec rails server
I also tried disabling the Content Security Policy in the settings dashboard.
I’ve looked on https://discourse.29th.local/logs/
but I’ve seen no errors, and nothing about Sidekiq.
On the Sidekiq note, I do have a message on the admin dashboard about updates:
A check for updates has not been performed. Ensure sidekiq is running.
So I ran Sidekiq.redis { |r| puts r.flushall }
in the rails console and got OK
, restarted the rails server, and no change to the message nor the overall problem. I’ve poked around in the redis cache and I don’t see anything about this page.
I’ve also attempted to simplify things by taking the nginx layer out of the equation: reverting SiteSetting.force_hostname
and SiteSetting.port
to nil
, turning off force https, accessing my web app and discourse via localhost
, and adding my web app to Discourse’s embeddable hosts and CORS hostnames (http://localhost:8080
), but I got the same error, just with different hosts:
Failed to execute 'postMessage' on 'DOMWindow': The target origin provided ('http://localhost:3000') does not match the recipient window's origin ('http://localhost:8080').
I’m running version 2.6.0.beta6 ( 60bc38e6a8 )
, which I got by cloning the master
branch as per the discourse for mac setup guide a couple weeks ago and running git pull origin master
today.
I’ve also removed the tmp
directory and restarted the server.
I’ve also gone for a walk, shouted into a pillow, and cried under my desk.
Hopefully this covers all the bases. I hope someone can help!