Discourse._registerPluginCode should never run code before objects are injected

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.

3 Likes

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

<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?

1 Like

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

4 Likes