Skip to content

fix: move getServerRoute to selectors module to avoid TDZ on plugin init#84

Merged
avasconcelos114 merged 2 commits into
mattermost-community:masterfrom
roberson-io:fix/initialize-tdz-error
Jul 16, 2026
Merged

fix: move getServerRoute to selectors module to avoid TDZ on plugin init#84
avasconcelos114 merged 2 commits into
mattermost-community:masterfrom
roberson-io:fix/initialize-tdz-error

Conversation

@roberson-io

Copy link
Copy Markdown
Contributor

Summary

Move getServerRoute from index.js into a new selectors.js module and import it, mirroring the structure of the GitHub, GitLab, and Jira plugins. Fixes a ReferenceError: Cannot access 'getServerRoute' before initialization thrown from Plugin.initialize() on Mattermost v11.x, which hangs the webapp on the loading spinner.

Also adds a regression test that mocks window.registerPlugin to synchronously invoke initialize() (mirroring modern Mattermost) and asserts the module loads without throwing. Temporarily reintroducing the original inline const declaration in index.js causes the test to fail with the same ReferenceError seen at runtime.

Ticket Link

Fixes #83

@roberson-io

Copy link
Copy Markdown
Contributor Author

@cpoile @hanzei @mickmister It looks like I don't have permission to add any reviewers. Would one of you mind taking a look at this when you have a chance? Thanks!

@avasconcelos114 avasconcelos114 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement! Just listed a couple of suggestions that would be nice to have alongside this fix

Comment thread webapp/src/selectors.js
Comment on lines +3 to +16
export const getServerRoute = (state) => {
const config = getConfig(state);

let basePath = '';
if (config && config.SiteURL) {
basePath = new URL(config.SiteURL).pathname;

if (basePath && basePath[basePath.length - 1] === '/') {
basePath = basePath.substr(0, basePath.length - 1);
}
}

return basePath;
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to have a test suite for this selector specifically as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avasconcelos114 I added selector tests.

Comment thread webapp/src/selectors.js
Comment on lines +7 to +13
if (config && config.SiteURL) {
basePath = new URL(config.SiteURL).pathname;

if (basePath && basePath[basePath.length - 1] === '/') {
basePath = basePath.substr(0, basePath.length - 1);
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nice to wrap this in a try-catch block to prevent any possible runtime errors due to a malformed SiteURL

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avasconcelos114 I wrapped this in a try/catch block.

@avasconcelos114 avasconcelos114 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for making the changes!

@avasconcelos114
avasconcelos114 merged commit e6d2b9a into mattermost-community:master Jul 16, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plugin fails to initialize on Mattermost v11.x with ReferenceError: Cannot access 'getServerRoute' before initialization

2 participants