# TypeError: Discourse.\_registerPluginCode is not a function

**URL:** https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572
**Category:** Support
**Created:** [28 mei 2018 om 08:06 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572 "2018-05-28T08:06:21Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![itsbhanusharma](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/itsbhanusharma/32/180717_2.png) [@itsbhanusharma](https://meta.discourse.org/u/itsbhanusharma)
#### Post date: [28 mei 2018 om 08:06 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/1 "2018-05-28T08:06:21Z")

</div>

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:

```js
<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](https://meta.discourse.org/t/53504?silent=true) as well.

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [13 januari 2019 om 20:28 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/2 "2019-01-13T20:28:01Z")

</div>

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:

- [Search results for 'Discourse.\_registerPluginCode is not a function' - Discourse Meta](https://meta.discourse.org/search?q=Discourse._registerPluginCode%20is%20not%20a%20function)
- [\_registerPluginCode is not a function](https://meta.discourse.org/t/-registerplugincode-is-not-a-function/55784)
- [Automatic backups are a hit or miss - #3 by itsbhanusharma](https://meta.discourse.org/t/automatic-backups-are-a-hit-or-miss/90181/3)
- [Discourse.\_registerPluginCode should never run code before objects are injected](https://meta.discourse.org/t/discourse--registerplugincode-should-never-run-code-before-objects-are-injected/62808)

## Symptom

When loading `/u/activate-account/<id>`, the browser croaks with this message to the console log:

```javascript
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](https://global.discourse-cdn.com/meta/original/3X/1/1/112c0c2a7f8607ddf11c10a55843ed99ac385740.png)

```javascript
» Discourse
» > Object { SiteSettings: {} }

```

### At a glance

 ![image](https://global.discourse-cdn.com/meta/original/3X/4/e/4ef100d7524da046d2edcbca693a12940c918f1d.png)

## Reason

We can **reproduce the behavior** with the [Brand Header](https://meta.discourse.org/t/brand-header-theme-component/77977) 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:

```plaintext
          (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:

 ![image](https://global.discourse-cdn.com/meta/original/3X/7/7/77256602a147828cb5ba9b804f2ddb98283a22ac.png)

With kind regards,  
Andreas.

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [13 januari 2019 om 21:03 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/3 "2019-01-13T21:03:21Z")

</div>

> [@amotl](#):
>
> ## Reason
> 
> We can **reproduce the behavior** on the `/u/activate-account` page with the [Brand header theme component](https://meta.discourse.org/t/brand-header-theme-component/77977) 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:

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 [https://github.com/discourse/discourse-brand-header/issues/1](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?

Thanks in advance,  
Andreas.

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [14 januari 2019 om 04:19 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/4 "2019-01-14T04:19:25Z")

</div>

[https://meta.discourse.org/t/brand-header-theme-component/77977/64?u=vinothkannansll/](https://meta.discourse.org/t/brand-header-theme-component/77977/64)

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.

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [15 januari 2019 om 02:57 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/5 "2019-01-15T02:57:03Z")

</div>

Thanks @vinothkannans for getting back and for pointing us to @sam’s comment. After following this to [JavaScript not loading on certain pages](https://meta.discourse.org/t/javascript-not-loading-on-certain-pages/92297) and drilling down a bit further we would like to share our findings.

> [@amotl](#):
>
> When looking at the `Discourse` object, it tells us it’s pretty much empty:
> 
> ```javascript
> » Discourse
> » > Object { SiteSettings: {} }
> 
> ```

While the origin of this pretty much void object pretending to be a real Discourse instance

```plaintext
const Discourse = {
  SiteSettings: {}
}

```

is yet unknown, it will easily sneak through some safeguards the Discourse plugin loader had put into place, so

> [@amotl](#):
>
> ```plaintext
> if ('Discourse' in window) { 
> Discourse._registerPluginCode('0.8', function (api) {
> // [...]
> }
> }
> 
> ```

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.

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [15 januari 2019 om 03:05 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/6 "2019-01-15T03:05:10Z")

</div>

… saying this, I don’t know yet how @sam’s comment at [FIX: don't include theme HTML in static pages by OsamaSayegh · Pull Request #6098 · discourse/discourse · GitHub](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:

> [@sam](#):
>
> […]
> 
> I think the simplest fix here is just to define this at the top:
> 
> ```plaintext
> <script>
> Discourse = { 
> _registerPluginCode: function() {};
> }
> <script>
> 
> ```
> 
> Then we correctly protect our core and don’t amend the behavior.

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?

---

<div class="post-metadata">

### Author: ![amotl](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/amotl/32/109873_2.png) [@amotl](https://meta.discourse.org/u/amotl)
#### Post date: [15 januari 2019 om 06:03 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/7 "2019-01-15T06:03:49Z")

</div>

By pulling together some [educated guessing](https://www.destroyallsoftware.com/talks/wat), improved [duck typing](https://en.wikipedia.org/wiki/Duck_typing) and a bit of [monkey patching](https://en.wikipedia.org/wiki/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.

> [@Almost void Discourse object instance will sneak through some safeguards](https://meta.discourse.org/t/almost-void-discourse-object-instance-will-sneak-through-some-safeguards/106553):
>
> Hi there, while investigating the root cause of [TypeError: Discourse.\_registerPluginCode is not a function - #2 by amotl](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/2), we found that while running through its initialization phase, Discourse introduces itself as a rather void object on all static or no\_ember pages (we learned that jargon while reading here, apologies when we’re getting something wrong) rather than being just undefined: » Discourse » \> Object { SiteSettings: {} } This will make it easy to sneak through safeguards like "if (…

## 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.

```plaintext
<!--
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>

```

---

<div class="post-metadata">

### Author: ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)
#### Post date: [23 januari 2019 om 11:05 UTC](https://meta.discourse.org/t/typeerror-discourse-registerplugincode-is-not-a-function/88572/8 "2019-01-23T11:05:33Z")

</div>

[https://meta.discourse.org/t/brand-header-theme-component/77977/98?u=vinothkannans](https://meta.discourse.org/t/brand-header-theme-component/77977/98)
