A known issue with the Brand Header theme component breaks the page layout on static and no-ember pages, such as 404 pages. The issue was first reported by @davisonio and has been discussed by @sam and @vinothkannans, who suggested rewriting the component to work on pages without ember.
The issue results in a TypeError: Discourse._registerPluginCode is not a function error message, which has been mitigated with a workaround. The workaround involves adding a script to reset the margin-top CSS property on the .d-header class, but it must be run in the Header phase of a theme component or later.
Screenshots of the issue are provided, along with links to related discussions on Meta Discourse, including a potential fix on GitHub.
We rediscovered an issue with the Brand Header which breaks the rendering on 404 pages and other pages without ember, already collected some background information about it and thought it would be a good idea to bring this up as a dedicated topic.
@sam and @vinothkannans already started a discussion about how to improve the plugin:
but sure there are many many things going on so this got lost. That’s why I’m picking this up again, maybe it could attract others working on it. If I will find some time, I would also like to have a look at the details.
One of the symptoms of this issue was the error message
TypeError: Discourse._registerPluginCode is not a function
revealing something along the way did not get initialized properly.
By following a specific hint found on GitHub from @sam (thanks again!), we have been able to work around the core problem which made Discourse plugin components croak on static and no_ember pages in general:
Learn about the brand header theme component
Before going to the next step, let’s investigate how the Brand Header manifests itself into the DOM. While most of its CSS (see common.scss) roams around with the .b-header class, there are two single CSS rules which just move the regular Discourse header a few pixels down, which we identified as a kind of an entrypoint regarding the CSS domain:
Thoughts
The problem looked like it was easy to tackle now.
By piggybacking on the workaround unlocked before, we would just visually toggle off the brand theme header by resetting the css property margin-top of the regular Discourse header through its .d-header class to its initial value, right? Challenge accepted ;].
That’s it
<!--
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
This resets the css property "margin-top" on the "d-header" class
to work around an obstacle with the banner header theme component.
-->
<script language="javascript">
if (!('Discourse' in window)) {
window.Discourse = {};
}
if ('Discourse' in window && !('_registerPluginCode' in window.Discourse)) {
window.Discourse._registerPluginCode = function() {
$(document).ready(function() {
$('.d-header').css('margin-top', 'initial');
});
};
}
</script>
Important note
Amending the margin-top css property will only work in the Header phase (or maybe later) of a theme component and after document.ready() has been signaled.
So, please run this from inside the Header phase or later (</head> will be too early!) of a theme component. Otherwise, it will not work.