A partial archive of https://discourse-mediawiki.wmflabs.org as of Saturday May 21, 2022.

How should I add a ResourceLoader module to most pages from an extension?

LucasWerkmeisterWMDE

I’m working on an extension that provides an API module and a ResourceLoader module which uses that API module. Currently the module is loaded as a gadget, but we’d like to enable it for all users soon. What’s the best way to add the module to all pages? The extension currently doesn’t see an OutputPage anywhere.

(Details: the extension is WikibaseQualityConstraints, the gadget is checkConstraints on Wikidata, and the task to enable it by default for everyone is T173695.)

addshore

I expect the best hook for that is "BeforePageDisplay"
Its description is “Allows last minute changes to the output page, e.g. adding of CSS or JavaScript by extensions.”

It provides you with an OutputPage object which you can call addModules() and addModuleStyles()

TheDJ

Note that it will be called for ALL pages, so be careful with your additions as it makes no distinctions between read, edit, special page, wiki text or interface message pages etc on its own, you will have to do that on your own (don’t assume the presence of certain elements, there is high variance with the content passed through this hook).

In these cases you should also be careful with your initial payload size, and only load what you actually need, before loading additional elements (Noting this, because in the past we had rollbacks on notifications for instance for failing to take this into account).