Page MenuHomePhabricator

Category-triggered gadgets not loaded in live preview
Open, Needs TriagePublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):

  1. Make sure the uselivepreview preference is enabled.
  1. Enable a gadget such as Calculator, which is defined to only load on particular category's pages:
Calculator [ResourceLoader | default | categories = Pages using gadget Calculator ] | calculator.js |calculator.css
  1. Open a page in that category for source editing, e.g. https://en.wikipedia.org/w/index.php?title=Template:Body_roundness_index&action=edit
  1. Click "Preview" (next to the Publish button) to load a live preview of the page.

What happens?:
The calculator does not appear.

What should have happened instead?:
The calculator appears, just as in non-live preview.

Other information (browser name/version, screenshots, etc.):
The parse API should return ext.gadget.Calculator as one of the modules.

Event Timeline

Ammarpad moved this task from Backlog to Live preview on the MediaWiki-Page-editing board.

The parse API should return ext.gadget.Calculator as one of the modules.

I believe the problem is the module presence (to be eligible for return) is dependent on category existence, but the category existence check has already happened on the first server loading of the edit page which is the only load for live preview. You can request the parsing API to return the categories of the page blindly, but then you really have to parse that info (maybe in MediaWiki-extensions-Gadgets) and dynamically load the module if it qualifies.

the category existence check has already happened on the first server loading of the edit page which is the only load for live preview

That would not be a problem. Live preview already asks for the categories as well as modules, which means the parse API is already capable of dynamically generating categories from a source. So it should just return the gadget as one of the modules if the triggering category is present in the parsed result.

The gadget in the example uses the wikipage.content hook so it loading only once is not a problem.

@SD0001 Can you look into this? The inability to live-preview template gadgets is frustrating.

The Parse API returns gadgets that have been added to the ParserOutput, not the OutputPage. The difference is that ParserOutput is agnostic of user preferences, skin, even the namespace and content model.

We could switch from BeforePageDisplay to ContentAlterParserOutput hook for category-triggered gadgets, but that comes at the cost of not being able to honour user opt-in/opt-out and the other conditional loading options.

but that comes at the cost of not being able to honour user opt-in/opt-out and the other conditional loading options.

A hack to do this would be to use RL\ClientHtml::getModules(), followed by removing the required gadgets, followed by RL\ClientHtml::setModules(). Neither OutputPage nor RL\ClientHtml expose a straightforward interface for removing modules.

Samwilson updated the task description. (Show Details)
Samwilson subscribed.

This doesn't seem related to Realtime Preview as that just uses the same core feature that live preview does. I've updated the description.

SD0001 renamed this task from Parse API does not return category-triggered gadgets to Category-triggered gadgets not loaded in live preview.Aug 29 2025, 3:18 AM

Change #1195386 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/core@master] ApiParse: initialize OutputPage with categories

https://gerrit.wikimedia.org/r/1195386

Change #1195386 merged by jenkins-bot:

[mediawiki/core@master] ApiParse: initialize OutputPage with categories

https://gerrit.wikimedia.org/r/1195386

SD0001 removed cscott as the assignee of this task.
SD0001 added a subscriber: cscott.

That was reverted unfortunately.

Change #1202980 had a related patch set uploaded (by SD0001; author: SD0001):

[mediawiki/extensions/Gadgets@master] Add gadgets using both OutputPageBeforeHTML and BeforePageDisplay hooks

https://gerrit.wikimedia.org/r/1202980