Users itsbhanusharma and amotl report a recurring JavaScript error — TypeError: Discourse._registerPluginCode is not a function — occurring specifically on the /u/activate-account page. The error stems from theme components (notably the Brand Header theme component) attempting to register plugins before the Discourse object is fully initialized. On affected pages, Discourse is prematurely defined as a minimal object ({ SiteSettings: {} }), causing plugin registration to fail amotl. vinothkannans acknowledges the issue and plans to fix it. In the meantime, amotl provides a workaround: a script snippet to be added in the theme’s Header phase that patches Discourse._registerPluginCode as a no-op if missing, preventing the error and allowing safeguards to function properly. The root cause is being investigated in a separate topic here.
When loading /u/activate-account/<id>, the browser croaks with this message to the console log:
TypeError: Discourse._registerPluginCode is not a function [Learn More] b6099b1e522f132eee5f0b5c866473e5dd008b46.js:7:3
<anonymous>
https://meta.ip-tools.org/theme-javascripts/b6099b1e522f132eee5f0b5c866473e5dd008b46.js:7:3
XML Parsing Error: syntax error report_js_error:1:1
Location: https://meta.ip-tools.org/logs/report_js_error
Line Number 1, Column 1:
When looking at the Discourse object, it tells us it’s pretty much empty:
We can reproduce the behavior with the Brand Header installed and activated. Without it, everything goes fine.
We can also confirm following the Javascript references that traces (where Discourse._registerPluginCode croaks) also lead towards the brand header theme component:
(function() {
if ('Em' in window) {
Ember.TEMPLATES["/connectors/above-site-header/brand-header"] = Ember.HTMLBars.template(...);
}
})();
if ('Discourse' in window) {
Discourse._registerPluginCode('0.8', function (api) {
var settings = { "brand_name": "The IP Software Community", "website_url": "https://meta.ip-tools.org/", "logo_url": "", "mobile_logo_url": "", "links": "", "icons": "home,http://www.ip-tools.org/|github,https://github.com/ip-tools/" };
[...]
Bummer
At our Discourse instance, this is especially annoying because the account authentication page doesn’t render properly after croaking:
maybe you are the right ones to talk to about an issue we are currently having with the brand header theme component you are authoring / have been contributing to? We just added it on GitHub at https://github.com/discourse/discourse-brand-header/issues/1.
It looks like the main Discourse instance is not yet available when being called by the brand header theme component on the account activation page. This smells like a timing issue re. resource initialization order.
Maybe you could have a look when you can afford some time?
While the origin of this pretty much void object pretending to be a real Discourse instance
const Discourse = {
SiteSettings: {}
}
is yet unknown, it will easily sneak through some safeguards the Discourse plugin loader had put into place, so
would obviously be completely clueless about this and will just attempt to run the plugin registration right away as if nothing happened, ultimately croaking there.
By pulling together some educated guessing, improved duck typing and a bit of monkey patching as @sam already might have suggested by his comment referenced above (thanks for the valuable hint!), we have been able to find the root cause and to provide a drop-in workaround.
Root cause
The root cause must be elsewhere, we just added an appropriate topic outlining our observations.
Workaround
When running this snippet in the Header phase (</head> will be too early!) of a theme component, it makes this error go away by monkey patching the ill-called _registerPluginCode method as a noop. The safeguards mentioned above will be able to do their jobs again and not croak on the browser console.
<!--
Work around `TypeError: Discourse._registerPluginCode is not a function`, see also:
- https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572
- https://meta.discourse.org/t/javascript-not-loading-on-certain-pages/92297
-->
<script language="javascript">
if (!('Discourse' in window)) {
window.Discourse = {};
}
if ('Discourse' in window && !('_registerPluginCode' in window.Discourse)) {
// Make this a noop
window.Discourse._registerPluginCode = function() {};
}
</script>