Javascript in velocity file no longer working in Jira 9 in certain views even when deferred

In order to get our app working in Jira 9, I have marked the javascript in my velocity file with type=“module” attribute that the Preparing for Jira 9.0 article suggested. This has allowed for the javascript to function properly on the main issue view. The problem is that the javascript does not function at all on other views, like the Open Issues view when a record is displayed in the right panel. Even worse, when you click the link to expand the record, it doesn’t seem to trigger any jira events, so it still won’t load the javascript.

Here is a paired down version of what we are doing:

panel.vm

$webResourceManager.requireResource("com.atlassian.auiplugin:ajs")
$webResourceManager.requireResource("com.atlassian.auiplugin:aui-experimental-table-sortable")
$webResourceManager.requireResource("com.atlassian.auiplugin:aui-spinner")
$webResourceManager.requireResource("com.custom:resources")

<script type="module">
	function loadWebPanel() {
		//process data from the context and update the various divs below
	}

    //Tried to bind to various jira events
	JIRA.bind(JIRA.Events.NEW_CONTENT_ADDED, function (e, context, reason) {
        if (reason=="pageload" || reason=="panelRefreshed"){
            loadWebPanel();
        }
    });
    
    JIRA.bind(JIRA.Events.ISSUE_REFRESHED, function (e, context) {
		loadWebPanel();
	});

	JIRA.bind(JIRA.Events.PANEL_REFRESHED, function (e, context) {
		loadWebPanel();
	});

    //Tried using the DOMContentLoaded
	document.addEventListener("DOMContentLoaded", loadWebPanel);
</script>

<!--context for displaying error messages-->
<div id="messageInformation"></div>

<div id="loadingInformation"><aui-spinner size="small"></aui-spinner>Please wait.</div>

None of these worked of course. Any ideas?

It could be related to [SPFE-909] - Ecosystem Jira

Agreed @remie. That issue and the Preparing for Jira 9 both suggest the same thing:

“The recommendation is to use the new ResourcePhase created in WRM 5.”

But I don’t know how to do that. Anyone out there have an example of how I use the ResourcePhase with the javascript in my vm file?

I’m coming to the conclusion that the underlying root cause of both SPFE-909 and the issue you are facing is that the new ResourcePhase has not been implemented for resources used in web-panel velocity templates. For some reason, these resources are not being picked up properly and are not included in the page.

Since upgrading to Jira 9, some users have reported that Javascript embedded in velocity files is no longer working in certain views. This can be a major problem for users who rely on this feature to display custom information or perform certain actions within Jira.

There are a few workarounds that have been successful in getting the Javascript to work again, but they may not work for everyone. One workaround is to add the following code to the velocity file:

#set ($deferJavascript=“true”)

This should tell Jira to defer loading of the Javascript until after the page has loaded, which should fix the issue. Another workaround is to add an extra space before the opening tag in the velocity file. This appears to cause Jira to load the script correctly.