# Discourse.\_registerPluginCode should never run code before objects are injected

**URL:** https://meta.discourse.org/t/discourse--registerplugincode-should-never-run-code-before-objects-are-injected/62808
**Category:** Development
**Created:** [May 16, 2017, 3:04pm UTC](https://meta.discourse.org/t/discourse--registerplugincode-should-never-run-code-before-objects-are-injected/62808 "2017-05-16T15:04:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [May 16, 2017, 3:04pm UTC](https://meta.discourse.org/t/discourse--registerplugincode-should-never-run-code-before-objects-are-injected/62808/1 "2017-05-16T15:04:46Z")

</div>

Currently themes transpile code in ` <script type="text/discourse-plugin" version="0.8">` to: `Discourse._registerPluginCode('0.8', function (api)`

This call often can happen before the application has initialized and thus `api.getCurrentUser` can be null if this is registered too early (eg in header).

We should change it so `registerPluginCode` always runs after all objects are injected into the context so the api is fully functional.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [May 16, 2017, 5:19pm UTC](https://meta.discourse.org/t/discourse--registerplugincode-should-never-run-code-before-objects-are-injected/62808/3 "2017-05-16T17:19:11Z")

</div>

I can’t seem to reproduce this. I added the following code to the header:

```html
<script type="text/discourse-plugin" version="0.8">
  console.log(api.getCurrentUser());
</script>

```

When I reload the page it outputs my user model. How can I reproduce this?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [May 16, 2017, 9:04pm UTC](https://meta.discourse.org/t/discourse--registerplugincode-should-never-run-code-before-objects-are-injected/62808/4 "2017-05-16T21:04:17Z")

</div>

Okay it looks like a lot of plugins were not executing after `inject-objects` which was caching `null`. This fixes it:

[https://github.com/discourse/discourse/commit/733b0009cb03e4cd061e82e295305897d4598845](https://github.com/discourse/discourse/commit/733b0009cb03e4cd061e82e295305897d4598845)

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [May 16, 2017, 9:13pm UTC](https://meta.discourse.org/t/discourse--registerplugincode-should-never-run-code-before-objects-are-injected/62808/5 "2017-05-16T21:13:17Z")

</div>


