TypeError: Discourse._registerPluginCode is not a function

(Bhanu Sharma) #1

I was Just casually checking the /logs of my forum and found a weird warning multiple times!

The /u/activate-account/{activation token}
Gives this error: TypeError: Discourse._registerPluginCode is not a function

Console points to this code:

<script>Discourse._registerPluginCode('0.8', function (api) {
    var settings = { "show_description": true, "show_mobile": true, "show_subcategory": true, "hide_if_no_description": false, "exceptions": "" };

    var container = Discourse.__container__;

    var _require = require('virtual-dom'),
        h = _require.h;

    api.createWidget('category-header-widget', {
        tagName: 'span',
        html: function html(attrs, state) {

            var path = window.location.pathname;
            var category = void 0;

            var controller = container.lookup('controller:navigation/category');
            category = controller.get("category");

            var isException = category && settings.exceptions.split("|").includes(category.name);

            if (/^\/c\//.test(path)) {
                var hideMobile = !settings.show_mobile && this.site.mobileView ? "true" : hideMobile;
                var subCat = !settings.show_subcategory && category.parentCategory ? "true" : subCat;
                var noDesc = settings.hide_if_no_description && !category.description_text ? "true" : noDesc;

                if (!isException && !noDesc && !subCat && !hideMobile) {
                    var catDesc = function catDesc() {
                        if (settings.show_description) {
                            return h('p', category.description_text);
                        }
                    };

                    $("body").addClass("category-header");

                    return h('div.category-title-header', {
                        "attributes": {
                            "style": "background-color: #" + category.color + "; color: #" + category.text_color + ";"
                        }
                    }, h('div.category-title-contents', [h('h1', category.name), catDesc()]));
                }
            } else {
                $("body").removeClass("category-header");
            }
        }
    }), api.decorateWidget('category-header-widget:after', function (helper) {
        helper.widget.appEvents.on('page:changed', function () {
            helper.widget.scheduleRerender();
        });
    });
});</script>

Is there anything to worry about or is it the expected behaviour?
All stock plugins, Repro possible in safe mode as well.

0 Likes

(Andreas Motl) #2

Hi there,

Background

We are getting the very same error, also on /u/activate-account, that’s why we arrived here. Others seem to already have experienced similar errors:

Symptom

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:
image

» Discourse
» > Object { SiteSettings: {} }

At a glance

Reason

We can reproduce the behavior with the Brand header theme component 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:

With kind regards,
Andreas.

1 Like

Almost void Discourse object instance will sneak through some safeguards
(Andreas Motl) #3

Dear @vinothkannans and @Johani,

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 Croaks with "TypeError: Discourse._registerPluginCode is not a function" · Issue #1 · discourse/discourse-brand-header · GitHub.

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?

Thanks in advance,
Andreas.

1 Like

(Vinoth Kannan) #4

I think this is very related to the above. It’s already in my list for long time. I will improve it soon. Thanks for the report.

1 Like

Brand header theme component
Brand header theme component breaks page layout on static and no_ember pages
(Andreas Motl) #5

Thanks @vinothkannans for getting back and for pointing us to @sam’s comment. After following this to JavaScript not loading on certain pages and drilling down a bit further we would like to share our findings.

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.

0 Likes

(Andreas Motl) #6

… saying this, I don’t know yet how @sam’s comment at https://github.com/discourse/discourse/pull/6098#issuecomment-405487611 (by the way, this is the change which introduced the if ('Discourse' in window) {} safeguards the other day) might be related or would fit into that picture:

Something like that feels like it’s a missing piece which would mitigate the issue we are currently seeing here. I just don’t see where

just [to] define this at the top

should happen exactly. In Discourse core on specific static / no_ember pages or on the plugin side?

0 Likes

(Andreas Motl) #7

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>
1 Like

Brand header theme component breaks page layout on static and no_ember pages
Brand header theme component
(Vinoth Kannan) closed #8
0 Likes